WPF数据绑定:如何绑定数据集? [英] WPF Data binding: How to data bind a collection?

查看:187
本文介绍了WPF数据绑定:如何绑定数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以读取我的解决方案的完整结构<一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:


  1. 我在实体类库做了一个类 Account.cs

  2. 我做了一个类库核心带班 AccountController.cs 这得到
    帐户从SQL Server表。

  3. 我发了 Gui.Wpf.Controllers 类库类 AccountWindowController.cs
    它包含列表与LT;帐户&GT;帐户属性,并呼吁 GetAccounts()
    方法在的AccountController 来填补这个名单。

  4. 最后,我在 Gui.Wpf 类库做了一个 AccountWindow.xaml 。该WPF窗口
    包含的ListBox 名为 AccountsListBox

  1. I made a class Account.cs in the Entities class library.
  2. I made a class library Core with a class AccountController.cs which gets the accounts from the Sql Server tables.
  3. I made a class AccountWindowController.cs in the Gui.Wpf.Controllers class library. It contains the List<Account> Accounts property and calls for the GetAccounts() method in the AccountController to fill that list.
  4. Finally, I made a AccountWindow.xaml in the Gui.Wpf class library. This WPF window contains a ListBox named AccountsListBox.

我要绑定数据从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆