代码之家  ›  专栏  ›  技术社区  ›  TerrorAustralis

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

  •  1
  • TerrorAustralis  · 技术社区  · 14 年前

    我有一个类,为了实验起见,称它为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数据模板显示栏的描述属性。 我试过简单的 <textblock Text="{Binding Path=barProp.Description}" /> 而且变种也无济于事

    寻求智慧,
    流行音乐播音员

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

    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元素的expander头中。

    2 回复  |  直到 14 年前
        1
  •  7
  •   Matt Hamilton    14 年前

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

        2
  •  0
  •   Johnny    14 年前

    试试这个

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

    希望这能奏效。 祝你好运!