【问题标题】:C# Populating list boxC# 填充列表框
【发布时间】:2017-04-28 00:57:08
【问题描述】:

我无法填充我的列表框。我最初从网站上的某个人那里得到了帮助,但似乎并没有完全发挥作用。我正在尝试使用文本文件中的项目填充列表框,这是到目前为止的代码:

namespace ACW2
{
    /// <summary>
    /// Interaction logic for InventoryWindow.xaml
    /// </summary>
    ///

    public partial class InventoryWindow : Window
    {
        public InventoryWindow()
        {
            InitializeComponent();

            categoryComboBox.Items.Add("All");
            categoryComboBox.Items.Add("Pizza");
            categoryComboBox.Items.Add("Burger");
            categoryComboBox.Items.Add("Sundry");
            categoryComboBox.SelectedValue = "All";

            PopulateList();
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


        }

        private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            PopulateList();
        }

        public void PopulateList()
        {
            listBox.Items.Clear();
            using (StreamReader sr = new StreamReader(@"inventory.txt"))
            {
                while (!sr.EndOfStream)
                {
                    for (int i = 0; i < 22; i++)
                    {
                        string StringListItem = sr.ReadLine();
                        if (!String.IsNullOrEmpty(StringListItem) &&
                           (categoryComboBox.SelectedItem != null &&
                           (StringListItem.Contains(categoryComboBox.SelectedItem.ToString()))))
                        listBox.Items.Add(StringListItem);
                    }
                }
            }
        }

    }
    }

它只是以一个空的列表框结束。然而,这段代码确实填充了列表框,但我觉得有两个 StreamReader 好像是多余的:

namespace ACW2
{
    /// <summary>
    /// Interaction logic for InventoryWindow.xaml
    /// </summary>
    ///

    public partial class InventoryWindow : Window
    {
        public InventoryWindow()
        {
            InitializeComponent();

            categoryComboBox.Items.Add("All");
            categoryComboBox.Items.Add("Pizza");
            categoryComboBox.Items.Add("Burger");
            categoryComboBox.Items.Add("Sundry");
            categoryComboBox.SelectedValue = "All";
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


        }

        private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            listBox.Items.Clear();

            StreamReader sr = new StreamReader("inventory.txt");
            string i = sr.ReadToEnd();
            string[] n = i.Split('\n');
            foreach (string s in n)
            {
                listBox.Items.Add(s);
            }
        }



        public void PopulateList()
        {
            listBox.Items.Clear();
            using (StreamReader sr = new StreamReader(@"inventory.txt"))
            {
                while (!sr.EndOfStream)
                {
                    for (int i = 0; i < 22; i++)
                    {
                        string StringListItem = sr.ReadLine();
                        if (!String.IsNullOrEmpty(StringListItem) &&
                           (categoryComboBox.SelectedItem != null &&
                           (StringListItem.Contains(categoryComboBox.SelectedItem.ToString()))))
                            listBox.Items.Add(StringListItem);
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 能发一下库存文件的格式吗?
  • 为什么是using (StreamReader sr = new StreamReader(@"inventory.txt")) 语法?你不能在没有{} 的情况下使用StreamReader sr = new StreamReader(@"inventory.txt"); 吗?
  • 古斯曼这只是一个列表:披萨,牛肉,0.50,500 披萨,鸡肉,0.50,500 披萨,面团,0.50,1500 披萨,蘑菇,0.25,500 披萨,橄榄,0.35,500汉堡, 牛肉饼, 0.66, 500 汉堡, 面包, 0.25, 500 汉堡, 切达干酪, 0.55, 500 杂货, 烤豆, 0.35, 500 杂货, 薯片, 0.33, 500
  • 我认为首先我会填充各个列表框的 Loaded 事件上的所有列表。这样您就可以 100% 确定知道列表框已准备好供您使用。

标签: c# wpf listbox populate inventory-management


【解决方案1】:
foreach (var line = File.ReadAllLines(@"inventory.txt"))
{
    listBox.Items.Add(line);
}

【讨论】:

    【解决方案2】:

    我不会从代码隐藏中执行此操作,而是设置一个 ViewModel,其中包含您的项目类型的 ObservableCollection(例如字符串或更强大的东西),并将 XAML 中的 ComboBox 绑定到该集合属性名称。

    这样您就可以更轻松地维护此应用程序 + 视图分离是免费的 :)

    // SampleViewModel.cs
    public ObservableCollection<string> Categories = new ObservableCollection<string>();
    
    // SampleView.xaml
    <Window.DataContext>
        <local:SampleViewModel/>
    </Window.DataContext>
    
    <ListView ItemSource="{Binding Categories}" />

    应该这样做:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多