2012-01-11 8 views
0

テキストボックスと送信ボタンがあるウィンドウがあります。送信ボタンを押すと、テキストボックスのデータがリストボックスに入力され、保存されます。Populate ListBox

これを行う最善の方法は何ですか?私は以前の質問からObservableCollectionを使って推薦を試みましたが、うまくいかないようです。

私はクラスを作成しました:

私のXAMLでバインディングを割り当て
public class AccountCollection 
{ 
    private string accountName; 
    public string AccountName 
    { 
     get { return accountName; } 
     set { accountName = value; } 
    } 
    public AccountCollection(string accountName) 
    { 
     AccountName = accountName; 
    } 
}   

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

しかし、悲しいかな、私は取得していますどこにもない。私はGetBindingExpression部にエラーメッセージが表示されます。

引数1:ここでは私には明らかだ何System.Windows.DependencyProperty "

に「文字列」から変換することはできませんと、「私は、クラスを作成したとき、私はdidnのことですテキストボックスからアカウント名について何も指定しないので、クラスが正しいかどうかわかりません。

私は基本的に混乱していて、何をすべきかわかりません。任意のヘルプは高く評価されます...

答えて

2

MODEL

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

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

CODE BEHIND

// 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はAccountNameです。これは文字列であり、コレクションではありません。あなたのクリックイベントハンドラで、

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

そして:

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

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

バインドのItemsSourceアカウントプロパティへ:あなたはこのように(ビューのためのあなたのDataContext)のviewmodelを作成する必要が

ボタンをクリックすると、テキストボックスの現在の値をコレクションに簡単に追加できます。

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

しかし、ウィンドウのDataContextをViewModelクラスに設定することを忘れないでください。ここ

+0

私はこのすべてをしたが、それはまだ動作していません。私は単純なものを逃しているに違いない。ここでの唯一の違いは、okBtn_ClickイベントでViewModelのインスタンスをインスタンス化しなければならないことです。 ViewModel vm =新しいViewModel(); vm.Accounts.Add(accountaddTextBox.Text); – Woody

+0

ウィンドウのDataContextを設定しましたか? 'mainWindow.DataContext = new ViewModel();'すべてのクリックではなく、viewmodelのインスタンスを作成するために必要なのは1回だけです! –

+0

はい。 'public Account() { InitializeComponent(); DataContext = new ViewModel(); } ' – Woody

2

ビューモデル

<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バインディング
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 MVVMのアプローチを使用してデモです。CSコード

# region Constructor 

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

    # endregion 

INotifyPropertyChangedの実装とporpetiesを形成することは残っている点で最大あなた