你可以看我的解决方案here的完整结构,但这里的快速参考:WPF数据绑定:如何将数据绑定到一个集合?
- 我在
Entities
类库做了一个类Account.cs
。 - 我做了一个类库
Core
与AccountController.cs
类从Sql Server表中获取 帐户。 - 我在
Gui.Wpf.Controllers
类库中做了AccountWindowController.cs
类的类。 它包含List<Account> Accounts
属性,并要求AccountController
中的GetAccounts()
方法填充该列表。 - 最后,我在
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);
}
}
谢谢你的帮助。
感谢您的回答马修。更改代码导致编译错误,并显示以下错误消息:Error解析标记扩展时遇到类型为“System.Windows.StaticResourceExtension”的未知属性“路径”。我应该改变别的东西吗? – Boris 2010-11-23 19:04:10
道歉,我错过了一些。请再试一次。 – 2010-11-23 20:34:44