首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >填充ListBox

填充ListBox
EN

Stack Overflow用户
提问于 2012-01-11 15:32:01
回答 3查看 1.2K关注 0票数 0

我有一个带有文本框和提交按钮的窗口。当按下submit按钮时,textbox中的数据应填充到列表框中并保存。

做这件事的最好方法是什么?我尝试了前面问题中的一个建议(使用ObservableCollection),但我似乎不能让它工作。我尝试过这样实现它:

我创建了一个类:

代码语言:javascript
复制
public class AccountCollection
{
    private string accountName;
    public string AccountName
    {
        get { return accountName; }
        set { accountName = value; }
    }
    public AccountCollection(string accountName)
    {
        AccountName = accountName;
    }
}        

在我的XAML中分配了绑定:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding AccountName, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" SelectionChanged="accountListBox_SelectionChanged" />

...and最后,当用户从包含submit按钮和文本框的另一个窗口中单击Submit按钮时:

代码语言:javascript
复制
private void okBtn_Click(object sender, RoutedEventArgs e)
{
    BindingExpression expression = okBtn.GetBindingExpression(accountaddTextBox.Text);
    expression.UpdateSource();
}

但遗憾的是,我一无所获。我在GetBindingExpression部分收到一条错误消息:

参数1:无法从'string‘转换为'System.Windows.DependencyProperty’

对我来说,显而易见的是,当我创建这个类时,我没有从文本框中指定任何关于帐户名的内容,所以我甚至不知道这个类是否正确。

我基本上很困惑,不知道该怎么办。任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-11 18:06:14

模型

代码语言:javascript
复制
// the model is the basic design of an object containing properties
// and methods of that object. This is an account object.

public class Account : 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));
      }
    }
}

ListBox XAML

代码语言:javascript
复制
 <ListBox Name="MyAccounts" DisplayMemberPath="AccountName" />

背后的代码

代码语言:javascript
复制
// create a collection of accounts, then whenever the button is clicked,
//create a new account object and add to the collection.

public partial class Window1 : Window
{
    private ObservableCollection<Account> AccountList = new ObservableCollection<Account>();

    public Window1()
    {
        InitializeComponent();
        AccountList.Add(new Account{ AccountName = "My Account" });
        this.MyAccounts.ItemsSource = AccountList;
    }
     private void okBtn_Click(object sender, RoutedEventArgs e)
    {
       AccountList.Add(new Account{ AccountName = accountaddTextBox.Text});
    }
}

编辑:在列表框xaml上添加显示成员路径

票数 2
EN

Stack Overflow用户

发布于 2012-01-11 16:27:47

这是一个使用MVVM方法的演示

ViewModel

代码语言:javascript
复制
public class AccountListViewModel : INotifyPropertyChanged
{

    ICommand AddAccountCommand {get; set;}

    public AccountListViewModel()
    {
        AccountList = new ObservableCollection<string>();
        AddAccountCommand= new RelayCommand(AddAccount);
        //Fill account List saved data
        FillAccountList();
    }

    public AddAccount(object obj)
    {
        AccountList.Add(AccountName); 
        //Call you Model function To Save you lIst to DB or XML or Where you Like
        SaveAccountList()   
    }

    public ObservableCollection<string> AccountList 
    { 
            get {return accountList} ; 
            set
            {
                accountList= value
                OnPropertyChanged("AccountList");
            } 
    }

    public string AccountName 
    { 
            get {return accountName } ; 
            set
            {
                accountName = value
                OnPropertyChanged("AccountName");
            } 
    }

}

Xaml绑定

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Path=AccountList}" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" />

<TextBox Text={Binding Path=AccountName}></TextBox>
<Button Command={Binding Path=AddAccountCommand}><Button>

Xaml.cs代码

代码语言:javascript
复制
    # region Constructor

    /// <summary>
    /// Default Constructor
    /// </summary>
    public MainView()
    {
        InitializeComponent();
        this.DataContext = new AccountListViewModel();
    }

    # endregion

INotifyPropertyChanged的实现和特性的形成就交给你了

票数 2
EN

Stack Overflow用户

发布于 2012-01-11 15:40:42

ListBox的ItemsSource是AccountName,它只是一个字符串,而不是一个集合。

您需要创建一个视图模型(视图的数据上下文),如下所示:

代码语言:javascript
复制
public class ViewModel
{
    public ViewModel()
    {
        Accounts = new ObservableCollection<string>();
    }

    public ObservableCollection<string> Accounts { get; set; }
}

将ItemsSource绑定到帐户属性:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Accounts}" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" />

然后,在按钮的click事件处理程序中,可以简单地将textbox的当前值添加到集合中:

代码语言:javascript
复制
private void okBtn_Click(object sender, RoutedEventArgs e)
{
    Accounts.Add(accountaddTextBox.Text);
}

但不要忘记将窗口的DataContext设置为ViewModel类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8815677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档