2010-11-23 22 views
0

あなたは私の解決策hereの完全な構造を読むことができるが、ここではクイックリファレンスです:WPFデータバインディング:コレクションにデータをバインドする方法は?

  1. 私はEntitiesクラスライブラリ内のクラスAccount.csを作りました。
  2. クラスライブラリCoreAccountController.csクラスにして、Sql Serverテーブルから アカウントを取得しました。
  3. 私はクラスAccountWindowController.csGui.Wpf.Controllersクラスライブラリに作成しました。 List<Account> Accountsプロパティが含まれており、そのリストを埋めるためにAccountControllerGetAccounts() メソッドを呼び出します。
  4. 最後に、Gui.WpfクラスライブラリでAccountWindow.xamlを作った。このWPFウィンドウ には、AccountsListBoxという名前のListBoxが含まれています。

私はデータにしたいがAccountWindowControllerAccountWindowからリストにリストボックスをバインドしますが、私は方法がわかりません。ここでは、関連するコードは次のとおりです。

AccountWindow.xaml

<Window x:Class="Gui.Wpf.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controller="clr-namespace:Gui.Wpf.Controllers" 
    Title="Accounts" 
    Width="350" 
    MinWidth="307" 
    MaxWidth="400" 
    Height="500" > 

    <Window.Resources> 
     <controller:AccountWindowController 
      x:Key="AccountsCollection" /> 
    </Window.Resources> 

    <Grid> 
     <ListBox 
      Name="AccountsListBox" 
      Margin="12,38,12,41" 
      ItemsSource="{StaticResource ResourceKey=AccountsCollection}" /> 
    </Grid> 

</Window> 

AccountWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     new Gui.Wpf.Controllers.AccountWindowController(); 
    } 
} 

AccountWindowController.cs

public class AccountWindowController 
{ 
    //This event is handled in the AccountController.cs 
    //that sets the Accounts property defined below. 
    public event EventHandler GetAccounts; 

    private List<Account> accounts; 
    public List<Account> Accounts 
    { 
     get 
     { 
      GetAccounts(this, new EventArgs()); 
      return accounts; 
     } 
     set 
     { 
      this.accounts = value; 
     } 
    } 

    //Constructor 
    public AccountWindowController() 
    { 
     new AccountController(this); 
    } 
} 

ありがとうございました。

答えて

1

中に、

ItemsSource="{StaticResource ResourceKey=AccountsCollection}" /> 

を変更してみてください。 AccountsCollectionリソースは、使用するプロパティを含むクラスです。これを実行するためには、そのプロパティにバインドし、バインディングのソースとしてリソースを使用する必要があります。

<ListBox Name="AccountsListBox" 
     Margin="12,38,12,41" 
     ItemsSource="{Binding Accounts, Source={StaticResource ResourceKey=AccountsCollection}}" /> 

あなたはまた、AccountWindowController にINotifyPropertyChangedを実装(およびアカウントのセッターでPropertyChangedを上げる)必要がありますAccountsプロパティを設定すると、ListBoxは新しいコレクションに再バインドされます。また、Accountsコレクションが実行時に変更された場合は、ObservableCollectionにする必要があります。

+0

安倍さん、ありがとうございました!今はすべてがわかります。とても有難い。 – Boris

0

あなたが近くにいるようです。 IEnumerableするItemsSourceニーズ

ItemsSource="{Binding Source={StaticResource ResourceKey=AccountsCollection}, Path=Accounts}" /> 
+0

答えてくれてありがとうMatthew。コードを変更すると、エラーメッセージでコンパイルエラーが発生しました。マークアップ拡張の解析中にエラーが発生しました。 'System.Windows.StaticResourceExtension'タイプの 'Path'が見つかりませんでした。私も他の何かを変えるべきですか? – Boris

+0

謝罪、私はちょっと寂しかった。もう一度お試しください。 –

関連する問題