将数据模板元素绑定到子类的属性

时间:2010-05-26 03:47:31

标签: c# wpf xaml binding datatemplate

我有一个班级,为了实验起见,将它称为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元素的扩展器头中

2 个答案:

答案 0 :(得分:7)

您的“AccountNumber”成员(“Field”类型)只是一个字段,而不是一个属性。您只能绑定到属性。给它一个吸气剂和制定者,它将开始工作。

答案 1 :(得分:0)

试试这个

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

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