【问题标题】:WPF Data binding: How to data bind a collection?WPF 数据绑定:如何对集合进行数据绑定?
【发布时间】:2011-05-14 15:21:21
【问题描述】:

您可以阅读我的解决方案 here 的完整结构,但这里是快速参考:

  1. 我在Entities 类库中创建了一个类Account.cs
  2. 我创建了一个类库Core 和一个类AccountController.cs,它得到了 Sql Server 表中的帐户。
  3. 我在Gui.Wpf.Controllers 类库中创建了一个类AccountWindowController.cs。 它包含List<Account> Accounts 属性并调用GetAccounts() AccountController 中的方法来填充该列表。
  4. 最后,我在Gui.Wpf类库中做了一个AccountWindow.xaml。这个 WPF 窗口 包含一个名为 AccountsListBoxListBox

我想将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


    【解决方案1】:

    看起来你快到了。尝试改变

    ItemsSource="{StaticResource ResourceKey=AccountsCollection}" />
    

    进入

    ItemsSource="{Binding Source={StaticResource ResourceKey=AccountsCollection}, Path=Accounts}" />
    

    【讨论】:

    • 感谢马修的回答。更改代码导致编译错误并显示错误消息:解析标记扩展时遇到类型“System.Windows.StaticResourceExtension”的错误未知属性“路径”。我是否也应该更改其他内容?
    • 抱歉,我错过了一点。请重试。
    【解决方案2】:

    ItemsSource 必须是 IEnumerableAccountsCollection 资源是一个包含您要使用的属性的类。为此,您需要绑定到该属性,并将资源用作绑定源:

    <ListBox Name="AccountsListBox"
             Margin="12,38,12,41" 
             ItemsSource="{Binding Accounts, Source={StaticResource ResourceKey=AccountsCollection}}" />
    

    您还应该在 AccountWindowController 上实现 INotifyPropertyChanged (并在 Accounts 设置器中引发 PropertyChanged),这样如果您设置 Accounts 属性,ListBox 将重新绑定到新集合。如果 Accounts 集合在运行时被修改,它应该是一个ObservableCollection

    【讨论】:

    • 安倍,非常感谢!我现在一切都清楚了,而且有效;非常感谢。
    猜你喜欢
    • 2011-01-03
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多