在窗口加载时填充列表框

本文关键字:列表 填充 窗口 加载 | 更新日期: 2023-09-27 18:20:12

这是我的情况。我有一个编码的解决方案,我在文本框中键入一个字符串,单击Add按钮后,它就会填充Listbox。

现在,我想:

a) 立即将该字符串保存到XML文件中。b) 当窗口打开时,我想在列表框中显示该XML文件的数据

到目前为止,我得到的是:

类别

    public class Accounts : INotifyPropertyChanged
    {
        private string m_AccountName;
        public event PropertyChangedEventHandler PropertyChanged;
        public string AccountName
        {
            get { return m_AccountName; }
            set
            {
                m_AccountName = value;
                OnPropertyChanged("AccountName");
            }
        }
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

代码隐藏

 public partial class Account : Window
    {
        private ObservableCollection<Accounts> AccountList = new ObservableCollection<Accounts>();
        public Account()
        {
            InitializeComponent();        
            this.accountListBox.ItemsSource = AccountList;
        }
        private void addBtn_Click(object sender, RoutedEventArgs e)
        {
            AccountList.Add(new Accounts { AccountName = accountaddTextBox.Text });
        }

XAML

<ListBox DisplayMemberPath="AccountName" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" />

此代码适用于在您单击"添加"按钮后填充列表框。

我已经尝试将XMLTextReader的实例添加到Window_Loaded中,并使用ArrayList尝试读取XML文件并加载它,但当我使用ItemsSource时,它会返回一个错误,即我必须使用ItemControl.ItemsSource…

以下是我尝试过的,但失败了:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {           
            XmlTextReader reader = new XmlTextReader("Accounts.xml");
            ArrayList ar = new ArrayList();
            //  Loop over the XML file
            while (reader.Read())
            {
                //  Here we check the type of the node, in this case we are looking for element
                if (reader.NodeType == XmlNodeType.Element)
                {
                    //  If the element is "accounts"
                    if (reader.Name == "Accounts")
                    {
                        ar.Add(reader.Value);
                        accountListBox.ItemsSource = ar;
                    }
                }
            }
            reader.Close();
        }

在窗口加载时填充列表框

将ViewModel作为AccountListViewModel

public class AccountListViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Accounts> accountList= 
                                           newObservableCollection<Accounts>();
    private string accountName;
    ICommand AddAccountCommand {get;set;}
    public event PropertyChangedEventHandler PropertyChanged;
    public AccountListViewModel()
    {
        ReadAllAccountsFromXml();
        AddAccountCommand=new  RelayCommand(AddAccountToListAndSave);
    }
    private void ReadAllAccountsFromXml()
    {           
        XmlTextReader reader = new XmlTextReader("Accounts.xml");

        //  Loop over the XML file
        while (reader.Read())
        {
            //  Here we check the type of the node, in this case we are looking for element
            if (reader.NodeType == XmlNodeType.Element)
            {
                //  If the element is "accounts"
                if (reader.Name == "Accounts")
                {
                    var account = new Accounts()
                    account.AccountName=reader.Value;
                    AccountList.Add(account)
                }
            }
        }
        reader.Close();
    }
    private void AddAccountToListAndSave(object obj)
    {
        var account = new Accounts();
        account.AccountName=AccountnName;
        AccountList.Add(account);
        SaveListToXml();
    }
    private void SaveListToXml()
    {
        //Write Xml Saving Code Here With Object as AccountList
    }
    public ObservableCollection<Accounts> AccountList
    {
        get { return accountList; }
        set
        {
            accountList = value;
            OnPropertyChanged("AccountList");
        }
    }
    public string AccountName
    {
        get { return accountnName; }
        set
        {
            accountnName = value;
            OnPropertyChanged("AccountName");
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

在您的Xaml.cs中

public partial class Account : Window
{
    private ObservableCollection<Accounts> AccountList = new ObservableCollection<Accounts>();
    public Account()
    {
        InitializeComponent();        
        this.DataContext= new AccountListViewModel();
    }
}

在您的Xaml中

<ListBox Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" ItemSource=AccountList>
<ListBox.ItemTemplate>
    <DataTemplate>
         <TextBlock Text={Binding Path=AccountName}/>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Text={Binding Path=AccountName,UpdateSourceTrigger=PropertyChanged}></TextBox>
<Button Command={Binding Path=AddAccountCommand}/>

我认为应该这样做…

这听起来像是您在将ArrayList绑定到ListBox时遇到了问题。

您的代码并不理想,但看起来应该可以工作。

试着运行下面的代码,看看它能给你什么。如果它有效,你可以尝试将其与项目中的代码进行比较,看看有什么不同。

ArrayList ar = new ArrayList();
ar.Add("hello");
ar.Add("world");
this.accountListBox.ItemsSource = ar;

如果你仍然没有任何运气,请发布错误的确切文本,这样我们就知道我们正在努力修复什么。。。