项目控制项目模板绑定

发布于 2024-09-06 09:20:40 字数 1840 浏览 2 评论 0原文

在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; }
}

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

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

然后,我从数据层获取显示信息的集合,并设置 ItemsControl 的 DataContext 到这个集合。 “Credit”属性显示正确,但 OwnerName 属性未正确显示。相反,我收到绑定错误:

错误 40:BindingExpression 路径 错误:找不到“OwnerName”属性 在“对象”“ForDisplay”上 (哈希码=449124874)'。 BindingExpression:路径=所有者名称; 数据项='用于显示' (哈希码=449124874);目标元素 是'TextBlock'(名称=txtOwnerName'); 目标属性是“文本”(类型 '字符串')

我不明白为什么它试图在 ForDisplay 类中查找 OwnerName 属性,而不是在 ForDisplay OwnerData 属性的 Owner 类中查找。

编辑 看来这与使用自定义控件有关。如果我将相同的属性绑定到 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>

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

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 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 DataContext of the ItemsControl to this collection. The "Credit" property gets displayed correctly, but the OwnerName property does not. Instead, I get a binding error:

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')

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.

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

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2024-09-13 09:20:40

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

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;

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文