【问题标题】:Binding Data Template element to property on sub-class将 Datatemplate 元素绑定到子类中的属性
【发布时间】:2011-02-23 23:57:03
【问题描述】:

我有一个类,为了实验起见称它为 foo() 和另一个类,称它为 bar()
我在我的 xaml 中定义了类 foo() 的数据模板,但 foo() 的属性之一是 bar() 对象,这样

foo()
{
    Public string Name {get; set;}
    Public int ID {get; set;}
    Public bar barProp {get; set;}
}

bar()
{
    Public string Description{get; set;}
}

我希望我的 foo 数据模板显示 bar 的 Description 属性。 我尝试了简单的<textblock Text="{Binding Path=barProp.Description}" /> 和变体无济于事

求智慧,
DJ

编辑: 根据要求提供更多信息...
这是我真正的课程...

public class AccountRecord
{
    public string Value { get; set; }
    public string Identifier { get; set; }
    public Field AccountNumber;
}
public class Field
{
    public string Name{get;set;}
    public string Value{get;set}
}

这是用于将它们模板化的 XAML...

<ListBox Margin="0,35,0,0" Name="listAccountRecords" Background="Transparent" BorderBrush="Transparent" ItemsSource="{Binding AccountRecords, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
    <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type lib:AccountRecord}">
                <Expander Header="{Binding AccountNumber.Name}">                               
                    <ListView ItemsSource="{Binding Fields}" ItemTemplate="{DynamicResource FieldTemplate}">
                    </ListView>
                </Expander>
            </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

确切的问题是 AccountNumber.Name 值没有显示在 AccountRecord 元素的扩展标题中

【问题讨论】:

  • 应该没问题。 WPF 的绑定系统完全支持嵌套属性。你看到了什么行为?您可能需要发布一个更具体的示例。
  • 检查 TextBlock.DataContext 属性 - 验证它是否设置为 Foo 类型的实例。绑定似乎是正确的。
  • 我已经添加了我正在处理的确切案例

标签: c# wpf xaml binding datatemplate


【解决方案1】:

您的“AccountNumber”成员(“字段”类型)只是一个字段,而不是一个属性。您只能绑定到属性。给它一个 getter 和 setter,它就会开始工作。

【讨论】:

  • @Terror 当涉及到 WPF 绑定时,它绝对是一个常见问题解答。它需要以某种方式更明显地记录下来。
【解决方案2】:

试试这个

<textblock Text="{Binding Path=FooObjectName.barProp.Description}" />

希望这会奏效.. 祝你好运!

【讨论】:

  • 感谢您的评论 :) 我已经尝试过了,当它不起作用时我很困惑,所以我认为有更险恶的东西在起作用。一个更完整的例子供您阅读:)
猜你喜欢
  • 2017-02-26
  • 2021-05-06
  • 2018-09-30
  • 1970-01-01
  • 2016-12-08
  • 2014-07-17
  • 2013-03-23
  • 2012-04-11
  • 1970-01-01
相关资源
最近更新 更多