Add()方法里面的if语句?

时间:2015-04-25 11:42:44

标签: c# wpf

我尝试将文本文件的每一行添加到listView中的不同列。但是,我遇到了一个问题。

这就是我做法的方法:

public void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string line = "";
            int index = 0;
            if (openFileDialog.ShowDialog() == true)
            using (StreamReader sr = File.OpenText(openFileDialog.FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    index++;
                    if (index == 1)
                        InvoiceNumbertxt.Text = line;
                    else if (index == 2)
                        InvoiceDatetxt.Text = line;
                    else if (index == 3)
                        DueDatetxt.Text = line;
                    else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                        PersonInfolst.Items.Add(line);
                    else if (index == 10)
                    {
                        Items.Add(new ItemProperties 
                        {     
                            Item = line
                            if(index == 11)// <---- If-statement inside Add?
                            Description = line;


                        });
                        itemlst.ItemsSource = Items;
                    }
                    else
                        break;
                }
            }
        }

正如你所看到的,index只是一个方便的标志(变量),用于按顺序插入行,而不是将多行重叠到同一个控件中。

问题我想检查索引是否是里面的值 Add()方法,以便我可以将新的textFile行添加到同一行但列表中的列不同。

更新

public partial class MainWindow : Window
    {
        ObservableCollection<ItemProperties> Items =
        new ObservableCollection<ItemProperties>();
        public MainWindow()
        {
            InitializeComponent();
        }
        public ObservableCollection<ItemProperties> GameCollection
        {
            get
            {
                if (Items == null)
                {
                    Items = new ObservableCollection<ItemProperties>();
                }
                return Items;
            }
        } 

        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string line = "";
            int index = 0;
            if (openFileDialog.ShowDialog() == true)
            using (StreamReader sr = File.OpenText(openFileDialog.FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    index++;
                    if (index == 1)
                        InvoiceNumbertxt.Text = line;
                    else if (index == 2)
                        InvoiceDatetxt.Text = line;
                    else if (index == 3)
                        DueDatetxt.Text = line;
                    else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                        PersonInfolst.Items.Add(line);
                    else if (index == 10)
                    {
                        Items.Add(new ItemProperties { Item = line });
                        itemlst.ItemsSource = Items;
                    }
                    else if (index == 11)
                    {
                        //??

                    }
                    else
                        break;
                }
            }
        }

        private void btnOpenImage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
            openfile.DefaultExt = "*.jpg";
            openfile.Filter = "Image Files|*.jpg";
            Nullable<bool> result = openfile.ShowDialog();
            if (result == true)
            {
                imagefile.Source = new BitmapImage(new Uri(openfile.FileName));
            }
        }

        public class ItemProperties
        {
            public string Item { get; set; }
            public string Description { get; set; }
            public string Quantity { get; set; }
            public string UnitPrice { get; set; }
            public string Tax { get; set; }
        }
    }

4 个答案:

答案 0 :(得分:1)

您可以使用三元运算符执行内联条件检查并设置值

Set

答案 1 :(得分:1)

您在Add() - Method中执行但在ItemProperties的对象初始值设定项内部。那里不可能!

最好的解决方案是编写额外的Add方法并在那里创建新的ItemProperties对象。

private MyAdd(string line, int index)
{
   if(index == 11)
      Items.Add(new ItemProperties {Item = line, Description = "line11"});
   else
      Items.Add(new ItemProperties {Item = line, Description = "other"});
}

然后

else if (index == 10)
{
    MyAdd(line, index);
    itemlst.ItemsSource = Items;
}

答案 2 :(得分:0)

首先,您应该考虑使用switch,因为它在这种情况下看起来很理想。多个ifs并不能满足您的需求。

其次,您可以使用简单的三元运算符来分配值,如下所示:

Description = id == 11 ? line : null;

答案 3 :(得分:0)

else if (index == 10)
{
    Items.Add(new ItemProperties{Item = line});
     itemlst.ItemsSource = Items;
}
else if (index == 11)
{
     Description = line;
}

忘记循环

InvoiceNumbertxt.Text = sr.ReadLine();
InvoiceDatetxt.Text = sr.ReadLine();
...
Items.Add(new ItemProperties 
                        {     
                            Item = sr.ReadLine();
                            Description = sr.ReadLine();
                        });