将字符串转换为int,将int转换为字符串

时间:2015-12-14 03:15:47

标签: c# wpf xaml

我正在使用添加了2个类的WPF文件。我有一个Movie类,一个Movie Utility类,MainWindow.xaml和MainWindow.xaml.cs。我的程序正在对.xaml窗口中输入的电影提出问题,并在下面生成数据网格。在Movie类中,我拥有所有公共字符串和日期行。像这样:

namespace FinalExam
{
    public class Movie
    {
        public string movieName { get; set; }

        public DateTime releaseDate { get; set; }

        public string onDVD { get; set; }

        public string onBluRay { get; set; }

        public string genreType { get; set; }

        public string directorName { get; set; }

        public string producerName { get; set; }

        public int movieLength { get; set; }

        public string moveRating { get; set; }        
    }
}

在MainWindow.xaml.cs类中,调用所有字符串,并使用ConvertStringToDate语法将日期行转换为字符串。

我遇到的问题是让MovieLength工作。我把它作为一个进入,但不知道如何让它出现在网格中,而不是它绊倒我的嘘声。无论如何,这就是我所拥有的。

namespace FinalExam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //GLOBAL VARIABLE AREA
        List<Movie> movieList = new List<Movie>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void but_Submit_Click(object sender, RoutedEventArgs e)
        {
            bool isGoodToAddNewMovie = true;
            Movie newMovie = new Movie();
            newMovie.movieName = txtBox_MovieName.Text;
            newMovie.onDVD = txtBox_DVD.Text;
            newMovie.onBluRay = txtBox_BluRay.Text;
            newMovie.genreType = txtBox_Genre.Text;
            newMovie.directorName = txtBox_Director.Text;
            newMovie.producerName = txtBox_Producer.Text;
            newMovie.moveRating = txtBox_Rating.Text;

            if (MovieUtility.isItDate(txtBox_ReleaseDate.Text))
            {
                newMovie.releaseDate = MovieUtility.ConvertStringToDate(txtBox_ReleaseDate.Text);
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.Red);
                isGoodToAddNewMovie = false;
            }

            if (MovieUtility.isItDate(txtBox_Length.Text))
            {
                newMovie.movieLength = MovieUtility.ConvertStringToDate(txtBox_Length.Text);
                txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_Length.Background = new SolidColorBrush(Colors.Red);
                MessageBox.Show("Please enter movie length in minutes.");
                isGoodToAddNewMovie = false;
            }

            //ADD PERSON TO LIST
            if (isGoodToAddNewMovie)
            {
                movieList.Add(newMovie);
                dataGrid_Movies.ItemsSource = new List<Movie>(movieList);
            }
        }
    }
}

这就是问题所在:

if (MovieUtility.isItDate(txtBox_Length.Text))
{
    newMovie.movieLength = MovieUtility.ConvertStringToDate(txtBox_Length.Text);
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}

2 个答案:

答案 0 :(得分:1)

您似乎只想将值解析为int,而不是DateTime。您可以先使用int.TryParse测试该值,如果该值为有效整数,则返回true

int length;

if (int.TryParse(txtBox_Length.Text, out length))
{
    newMovie.movieLength = length;
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}
else
{
    txtBox_Length.Background = new SolidColorBrush(Colors.Red);
    MessageBox.Show("Please enter movie length in minutes.");
    isGoodToAddNewMovie = false;
}

答案 1 :(得分:0)

看起来你想要Int32.TryParse

if (Int32.TryParse(txtBox_Length.Text, out newMovie.movieLength))
{
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}