2012-01-11 181 views
0

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

这样做的最好方法是什么?我尝试了一个建议(使用ObservableCollection)从我以前的问题,但我似乎无法得到它的工作。我曾尝试实施这样的:

我创建了一个类:

public class AccountCollection 
{ 
    private string accountName; 
    public string AccountName 
    { 
     get { return accountName; } 
     set { accountName = value; } 
    } 
    public AccountCollection(string accountName) 
    { 
     AccountName = accountName; 
    } 
}   

分配在我的XAML绑定:

<ListBox ItemsSource="{Binding AccountName, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" SelectionChanged="accountListBox_SelectionChanged" /> 

......最后,当用户点击提交从另一个窗口,包含提交按钮和文本框的按钮:

private void okBtn_Click(object sender, RoutedEventArgs e) 
{ 
    BindingExpression expression = okBtn.GetBindingExpression(accountaddTextBox.Text); 
    expression.UpdateSource(); 
} 

但唉,我越来越无处。

参数1:我在GetBindingExpression部分得到错误信息无法从“字符串”转换为“System.Windows.DependencyProperty”

什么是明显,我这里要说的是,当我创建的类我没有”从文本框中指定帐户名称的任何内容,所以我甚至不知道该类是否正确。

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

回答

2

模型

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

列表框XAML

<ListBox Name="MyAccounts" DisplayMemberPath="AccountName" /> 

后面的代码

// 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添加的DisplayMemberPath

1

你对于自己的ListBox的ItemsSource的帐户名,这仅仅是一个字符串,但不是一个集合。

你需要创建一个视图模型(你的datacontext为视图)是这样的:

public class ViewModel 
{ 
    public ViewModel() 
    { 
     Accounts = new ObservableCollection<string>(); 
    } 

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

绑定的ItemsSource到帐户属性:

<ListBox ItemsSource="{Binding Accounts}" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" /> 

,然后在的点击事件处理按钮,您可以简单地将文本框的当前值添加到您的收藏中:

private void okBtn_Click(object sender, RoutedEventArgs e) 
{ 
    Accounts.Add(accountaddTextBox.Text); 
} 

但是别忘了将窗口的DataContext设置为类ViewModel。

+0

我所做的这一切,但它仍然没有工作。我必须错过简单的东西。唯一的区别是我必须在okBtn_Click事件中实例化一个ViewModel的实例。 ViewModel vm = new ViewModel(); vm.Accounts.Add(accountaddTextBox.Text); – Woody 2012-01-11 08:04:16

+0

你是否设置了你的窗口的DataContext? 'mainWindow.DataContext = new ViewModel();'你只需要一次创建viewmodel的实例,而不是每次点击! – 2012-01-11 08:08:32

+0

是的。 'public account() { InitializeComponent(); DataContext = new ViewModel(); }' – Woody 2012-01-11 08:20:16

2

下面是使用MVVM方法

视图模型
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绑定

<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代码

# region Constructor 

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

    # endregion 

INotifyPropertyChanged的实现和形成porpeties留给你高达