在窗口加载时填充列表框

时间:2012-01-14 04:32:07

标签: c# wpf

这是我的情况。我有一个解决方案编码,我在一个文本框中键入一个字符串,点击一个添加按钮,它填充列表框。

现在,我想:

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();
        }

2 个答案:

答案 0 :(得分:1)

将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}/>

我相信这应该做到....

答案 1 :(得分:0)

听起来你只是在将ArrayList绑定到ListBox时遇到了麻烦。

你的代码并不理想,但它看起来应该有效。

尝试运行以下代码,看看它给你的是什么。如果它有效,您可以尝试将其与项目中的代码进行比较,看看它们之间的差异。

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

如果您还没有运气,请发布错误的确切文字,以便我们知道我们要修复的内容......