あなたは私の解決策hereの完全な構造を読むことができるが、ここではクイックリファレンスです:WPFデータバインディング:コレクションにデータをバインドする方法は?
- 私は
Entities
クラスライブラリ内のクラスAccount.cs
を作りました。 - クラスライブラリ
Core
をAccountController.cs
クラスにして、Sql Serverテーブルから アカウントを取得しました。 - 私はクラス
AccountWindowController.cs
をGui.Wpf.Controllers
クラスライブラリに作成しました。List<Account> Accounts
プロパティが含まれており、そのリストを埋めるためにAccountController
のGetAccounts()
メソッドを呼び出します。 - 最後に、
Gui.Wpf
クラスライブラリでAccountWindow.xaml
を作った。このWPFウィンドウ には、AccountsListBox
という名前のListBox
が含まれています。
私はデータにしたいがAccountWindowController
にAccountWindow
からリストにリストボックスをバインドしますが、私は方法がわかりません。ここでは、関連するコードは次のとおりです。
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);
}
}
ありがとうございました。
安倍さん、ありがとうございました!今はすべてがわかります。とても有難い。 – Boris