【问题标题】:Binding to collections in WPF绑定到 WPF 中的集合
【发布时间】:2013-04-25 22:19:11
【问题描述】:

所以我最终决定从 WinForms 转移到 WPF,我的旅程相当有趣。我有一个简单的应用程序,我将ObservableCollection 绑定到ListBox

我有一个Animal 实体:

namespace MyTestApp
{
    public class Animal
    {
        public string animalName;
        public string species;

        public Animal()
        {
        }

        public string AnimalName { get { return animalName; } set { animalName = value; } }
        public string Species { get { return species; } set { species = value; } }
    }
}

还有一个AnimalList 实体:

namespace MyTestApp
{
    public class AnimalList : ObservableCollection<Animal>
    {
        public AnimalList() : base()
        {
        }
    }
}

最后这是我的主窗口:

<Window x:Class="MyTestApp.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyTestApp"
    Title="Window3" Height="478" Width="563">

<Window.Resources>
    <local:AnimalList x:Key="animalList">
        <local:Animal AnimalName="Dog" Species="Dog"/>
        <local:Animal AnimalName="Wolf" Species="Dog"/>
        <local:Animal AnimalName="Cat" Species="Cat"/>
    </local:AnimalList>    
</Window.Resources>

<Grid>
    <StackPanel Orientation="Vertical" Margin="10,0,0,0">
        <TextBlock FontWeight="ExtraBold">List of Animals</TextBlock>
        <ListBox ItemsSource="{Binding Source={StaticResource animalList}, Path=AnimalName}"></ListBox>
    </StackPanel>
</Grid>

现在,当我运行应用程序时,我看到列表框填充了三个项目:“D”、“o”和“g”,而不是“Dog”、“Wolf”和“Cat”:

我有一种强烈的感觉,我在某处做了一些愚蠢的事情(也许是 AnimalList 构造函数?)但我不知道它是什么。任何帮助表示赞赏。

【问题讨论】:

    标签: c# wpf xaml data-binding observablecollection


    【解决方案1】:

    您需要设置 DisplayMemberPath(与绑定中的 Path 属性相反)。

    <Grid>
        <StackPanel Orientation="Vertical" Margin="10,0,0,0">
            <TextBlock FontWeight="ExtraBold">List of Animals</TextBlock>
            <ListBox ItemsSource="{Binding Source={StaticResource animalList}}" DisplayMemberPath="AnimalName"></ListBox>
        </StackPanel>
    </Grid>
    

    由于您要绑定到 Animal 对象的列表,DisplayMemberPath 指定要显示为列表项的 Animal 类中的属性名称。

    如果属性本身是一个对象,您可以使用点表示法来指定要显示的属性的完整路径,即..

    <ListBox ItemsSource="{Binding Source={StaticResource animalList}}" DisplayMemberPath="PropertyInAnimalClass.PropertyInTheChildObject.PropertyToDisplay" />
    

    【讨论】:

      【解决方案2】:

      您将列表框绑定到动物名称。相反,您应该将列表框绑定到您的集合:

      <ListBox ItemsSource="{Binding Source={StaticResource animalList}}"></ListBox>
      

      请注意,我已从绑定中删除了 path=AnimalName

      现在您将看到类名,因为 ListBox 不知道如何显示 Animal,因此它调用了它的 ToString 方法。

      你可以通过给它一个 ItemTemplate 来解决这个问题:

      <ListBox ItemsSource="{Binding Source={StaticResource animalList}}">
          <ListBox.ItemTemplate>
              <DataTemplate>
                  <StackPanel>    
                      <TextBlock Text="{Binding AnimalName}" />
                  </StackPanel>
              </DataTemplate>
          </ListBox.ItemTemplate>
      </ListBox>
      

      在 itemtemplate 中,您的 DataContext 是 Animal 的一个实例,然后您可以绑定到该实例上的属性。在我的示例中,我绑定了 AnimalName,但您基本上可以使用普通 XAML 控件构建任何您想要的模板,并绑定到绑定对象的不同属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        • 2011-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多