【问题标题】:Bind a collection to a WPF ListBox将集合绑定到 WPF 列表框
【发布时间】:2011-07-18 20:45:46
【问题描述】:

更新:到目前为止,我已经根据您的帮助更新了代码,但仍然没有成功。当应用程序加载时,ListBox 没有任何项目。我在 windows 的构造函数中为客户分配垃圾值,然后我还尝试将 ListBox 的 DataContext 设置如下:

CustomerList.DataContext = Customers;

--- 原始问题(带有更新的代码)---

我在 WPF 项目中遇到数据绑定问题。

我有一个类 Customer,如下所示:

public class Customer
{
    public String Name { get; set; }    
    public String Email { get; set; }
}

在我背后的 XAML 代码中,我有一组客户如下:

public List<Customer> Customers { get; set; }

我正在尝试使用 ListItemTemplate 将每个客户绑定到一个 ListBox,该 ListItemTemplate 在 TextBoxes 中显示客户的信息(姓名/电子邮件)以及一个锁定/解锁 TextBoxes 的按钮(将 IsEnabled 属性设置为 true 或 false)。

最好的方法是什么?

到目前为止,我一直在尝试追随他,但没有成功。

在 XAML 中,我目前有以下内容(暂时忽略切换部分,我只是想让集合本身被列出。):

<Window.Resources>
    <CollectionViewSource x:Key="Customers" Source="{Binding Path=Customers, Mode=TwoWay}"/>
    <DataTemplate x:Key="Customer">
        <StackPanel Orientation="Horizontal">
            <TextBox Content="{Binding Name}" />
            <TextBox Content="{Binding Email}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<StackPanel>
    <ListBox ItemsSource="{Binding Source={StaticResource Customers}}"
             ItemTemplate="{StaticResource ResourceKey=Customer}"
             Name="CustomerList"
             Height="300" />
</StackPanel>

【问题讨论】:

  • Customers 是字段还是属性?要使绑定起作用,它必须是一个属性。
  • 您为您的CollectionViewSource 和您的DataTemplate 提供了相同的密钥。将密钥更改为其中一个独特的东西。
  • @ChrisF,当我运行应用程序时,ListBox 中没有显示任何项目,所以我假设数据绑定由于某些原因无法正常工作。
  • @svick,Customers 是 MainWindow.xaml.cs 的一个属性
  • 我认为您不需要指定 List 的 DataContext,它不使用它(因为它指定了源),而是将窗口设置为包含 Customers 的任何对象,因为在 CollectionViewSource 中您只指定了一个路径,因此 DataContext 将用作基本源。如果您不熟悉这一切,请阅读 MSDN 上的文章,例如 Data Binding Overview

标签: c# wpf xaml data-binding


【解决方案1】:

你需要改变

ItemsSource="{Binding Source=Customers}"

ItemsSource="{Binding Source={StaticResource Customers}}" DataContext="{StaticResource Customers}"

【讨论】:

  • 或 ItemsSource="{Binding Source={StaticResource Customers}}"
  • 实际上只有 Jerry 的版本应该可以工作,因为你不能直接使用CollectionViewSource 作为ItemsSource。此外,需要在某处设置 DataContext。
  • @H.B.我应该如何设置 DataContext?我目前正在设置它,如我在窗口构造函数中更新的问题中所示。那是对的吗?谢谢!
  • @Jerry Nixon,@Matt Eleen,感谢您的帮助!我已经按照更新的问题中的说明进行了更改,但我似乎仍然无法使绑定工作。虽然我无法设置 DataSource,但我不认为它是 ListBox 的属性,应该在哪里设置?谢谢!
  • @Evan:对不起,我拼错了 datacontext。我着急了。
【解决方案2】:

更改后类似于更新的代码对我有用

<TextBox Content="{Binding Name}" />

<TextBox Text="{Binding Name}" />

由于TextBox 没有Content 属性(如Label),前者拒绝在VS 中编译。

嗯,它在定义中设置为Text

[ContentPropertyAttribute("Text")]
public class TextBox : TextBoxBase, IAddChild

但我以为它只用在括号之间(&lt;TextBox&gt;Like so&lt;/TextBox&gt;)?

这可能是问题的根源吗?

【讨论】:

    【解决方案3】:

    尝试如下设置 CustomerList 的 ItemsSourceItemsSource="{Binding}"。您已将 ListBox 的 DataContext 设置为客户列表,您需要将 ItemsSource 设置为相同的集合,因此直接绑定。

    如果您更喜欢使用CollectionViewSource,您可以做的另一件事是将窗口的DataContext 设置为同一类DataContext=this,因为没有这个,资源定义将无法找到您在后面的代码中定义的“客户”集合。但是,如果您这样做,则不需要 CustomerList.DataContext = Customers;,因为您直接将 ItemsSource 分配给静态资源,而不是相对于 DataContext。

    还有一件事。我认为您应该在不同名称后面的代码中给出 CollectionViewSource 和相应的集合。这不会导致运行时问题,但会导致代码难以维护;)

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 2018-10-23
      • 1970-01-01
      • 2011-06-05
      • 2011-08-28
      • 2013-05-21
      相关资源
      最近更新 更多