Binding Data Template element to property on sub-class
I have a class, for experiment sake call it foo() and another class, call it bar()
I have a data template for class foo() defined in my xaml, but one of foo()'s properties is a bar() object such thatfoo()
{
Public string Name {get; set;}
Public int ID {get; set;}
Public bar barProp {get; set;}
}
and
bar()
{
Public string Description{get; set;}
}
I want my data template of foo to display the Description property of bar.
I have tried the simple <textblock Text="{Binding Path=barProp.Description}" />
and variants to no avail
Seeking wisdom,
DJEDIT: In accordance with requests for more information...
here are my real classes...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}
}
and here is the XAML used to template them them...
<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>
And the exact problem is that the AccountNumber.Name value is not showing up in the expander header for the AccountRecord element
Your "AccountNumber" member (of type "Field") is only a field, not a property. You can only bind to properties. Give it a getter and setter and it'll start working.
Try This
<textblock Text="{Binding Path=FooObjectName.barProp.Description}" />
Hope this will work.. Good Luck !
精彩评论