【发布时间】:2011-05-14 15:21:21
【问题描述】:
您可以阅读我的解决方案 here 的完整结构,但这里是快速参考:
- 我在
Entities类库中创建了一个类Account.cs。 - 我创建了一个类库
Core和一个类AccountController.cs,它得到了 Sql Server 表中的帐户。 - 我在
Gui.Wpf.Controllers类库中创建了一个类AccountWindowController.cs。 它包含List<Account> Accounts属性并调用GetAccounts()AccountController中的方法来填充该列表。 - 最后,我在
Gui.Wpf类库中做了一个AccountWindow.xaml。这个 WPF 窗口 包含一个名为AccountsListBox的ListBox。
我想将AccountWindow 中的列表框数据绑定到AccountWindowController 中的列表,但我不知道怎么做。以下是相关代码:
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);
}
}
感谢您的所有帮助。
【问题讨论】:
标签: wpf data-binding binding