簡體   English   中英

C#填充列表框

[英]C# Populating list box

我在填充列表框時遇到問題。 最初,我從網站上的某個人那里獲得了幫助,但似乎並未充分發揮作用。 我正在嘗試用文本文件中的項目填充列表框,到目前為止,這是代碼:

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);
                    }
                }
            }
        }
    }
}
foreach (var line = File.ReadAllLines(@"inventory.txt"))
{
    listBox.Items.Add(line);
}

與其從背后的代碼中進行操作,不如使用您的項目類型(例如字符串或更健壯的對象)的ObservableCollection設置一個ViewModel,然后將ComboBox從XAML綁定到該集合屬性名稱。

這樣,您將可以更輕松地維護該應用程序,並且視圖分離是免費的:)

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

這應該做:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM