WPF数据绑定:如何绑定数据集? [英] WPF Data binding: How to data bind a collection?
问题描述
您可以读取我的解决方案的完整结构<一href=\"http://stackoverflow.com/questions/4257542/wpf-data-binding-how-to-organize-projects-and-classes-in-a-solution\">here,但这里的快速参考:
You may read the complete structure of my solution here, but here's the quick reference:
- 我在
实体
类库做了一个类Account.cs
。 - 我做了一个类库
核心
带班AccountController.cs
这得到
帐户从SQL Server表。 - 我发了
Gui.Wpf.Controllers
类库类AccountWindowController.cs
。
它包含列表与LT;帐户&GT;帐户
属性,并呼吁GetAccounts()
方法在的AccountController
来填补这个名单。 - 最后,我在
Gui.Wpf
类库做了一个AccountWindow.xaml
。该WPF窗口
包含的ListBox
名为AccountsListBox
。
- I made a class
Account.cs
in theEntities
class library. - I made a class library
Core
with a classAccountController.cs
which gets the accounts from the Sql Server tables. - I made a class
AccountWindowController.cs
in theGui.Wpf.Controllers
class library. It contains theList<Account> Accounts
property and calls for theGetAccounts()
method in theAccountController
to fill that list. - Finally, I made a
AccountWindow.xaml
in theGui.Wpf
class library. This WPF window contains aListBox
namedAccountsListBox
.
我要绑定数据从 AccountWindow
列表框在 AccountWindowController
,但我不清单知道如何。下面是相关的code:
I want to data bind the list box from AccountWindow
to the list in the AccountWindowController
, but I don't know how. Here's the relevant code:
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);
}
}
感谢您的所有帮助。
推荐答案
的的ItemsSource
需要有一个的IEnumerable
。在 AccountsCollection
资源是包含您要使用的属性的类。为了做到这一点,你需要绑定到该属性,使用资源的绑定的来源:
The ItemsSource
needs to be an IEnumerable
. The AccountsCollection
resource is a class that contains the property you want to use. In order to do this, you need to bind to that property, and use the resource as the source of the binding:
<ListBox Name="AccountsListBox"
Margin="12,38,12,41"
ItemsSource="{Binding Accounts, Source={StaticResource ResourceKey=AccountsCollection}}" />
您也应该实施 INotifyPropertyChanged的
在AccountWindowController
(并提高的PropertyChanged
中的帐户二传手),因此,如果您设置帐户属性中,的ListBox
将重新绑定到新系列。而如果账户集合在运行时修改,它应该是一个的ObservableCollection
。
You should also implement INotifyPropertyChanged
on the AccountWindowController
(and raise PropertyChanged
in the Accounts setter) so that if you set the Accounts property, the ListBox
will rebind to the new collection. And if the Accounts collection is modified at runtime, it should be an ObservableCollection
.
这篇关于WPF数据绑定:如何绑定数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!