wpf ItemsControl ItemTemplate 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3063031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:52:30  来源:igfitidea点击:

ItemsControl ItemTemplate Binding

wpfbinding.net-4.0itemscontrolitemtemplate

提问by Wonko the Sane

In WPF4.0, I have a class that contains other class types as properties (combining multiple data types for display). Something like:

在 WPF4.0 中,我有一个包含其他类类型作为属性的类(组合多种数据类型进行显示)。就像是:

public partial class Owner
{
     public string OwnerName { get; set; }
     public int    OwnerId   { get; set; }
}

partial class ForDisplay
{
    public Owner OwnerData { get; set; }
    public int Credit { get; set; }
}

In my window, I have an ItemsControl with the following (clipped for clarity):

在我的窗口中,我有一个 ItemsControl,其中包含以下内容(为清晰起见进行了剪辑):

<ItemsControl ItemsSource={Binding}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <local:MyDisplayControl 
                OwnerName={Binding OwnerData.OwnerName}
                Credit={Binding Credit} />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I then get a collection of display information from the data layer, and set the DataContextof the ItemsControlto this collection. The "Credit" property gets displayed correctly, but the OwnerName property does not. Instead, I get a binding error:

然后我得到的数据层显示信息的集合,并设置DataContextItemsControl这个集合。“Credit”属性正确显示,但 OwnerName 属性没有正确显示。相反,我收到一个绑定错误:

Error 40: BindingExpression path error: 'OwnerName' property not found on 'object' ''ForDisplay' (HashCode=449124874)'. BindingExpression:Path=OwnerName; DataItem='ForDisplay' (HashCode=449124874); target element is 'TextBlock' (Name=txtOwnerName'); target property is 'Text' (type 'String')

错误 40:BindingExpression 路径错误:在“对象”“ForDisplay”(HashCode=449124874)上找不到“OwnerName”属性。BindingExpression:Path=OwnerName; DataItem='ForDisplay' (HashCode=449124874); 目标元素是 'TextBlock' (Name=txtOwnerName'); 目标属性是“文本”(类型“字符串”)

I don't understand why this is attempting to look for the OwnerName property in the ForDisplay class, rather than in the Owner class from the ForDisplay OwnerData property.

我不明白为什么这会尝试在 ForDisplay 类中查找 OwnerName 属性,而不是在来自 ForDisplay OwnerData 属性的 Owner 类中。

EditIt appears that it has something to do with using the custom control. If I bind the same properties to a TextBlock, they work correctly.

编辑看来这与使用自定义控件有关。如果我将相同的属性绑定到 a TextBlock,它们会正常工作。

<ItemsControl ItemsSource={Binding}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <StackPanel>
              <local:MyDisplayControl 
                        OwnerName={Binding OwnerData.OwnerName}
                        Credit={Binding Credit} />
              <TextBlock Text="{Binding OwnerData.OwnerName}" />
              <TextBlock Text="{Binding Credit}" />
          </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

回答by decyclone

Are you sure the code you posted here IS the code you use in your solution? Because, this code works for me :

您确定您在此处发布的代码是您在解决方案中使用的代码吗?因为,这段代码对我有用:

XAML

XAML

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding OwnerData.OwnerName}"></TextBlock>
                <TextBlock Text="{Binding Credit}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Window's Loaded Event

窗口的加载事件

ObservableCollection<ForDisplay> items = new ObservableCollection<ForDisplay>();

for (int i = 0; i < 10; i++)
{
    items.Add(new ForDisplay() { OwnerData = new Owner() { OwnerId = i + 1, OwnerName = String.Format("Owner #{0}", i + 1) }, Credit = i + 1 });
}

DataContext = items;