1

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 that

foo()
{
    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,
DJ

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

3
  • That should work just fine. Nested properties are totally supported in WPF's binding system. What behaviour are you seeing? You might need to post a more concrete example. Commented May 26, 2010 at 3:50
  • Check the TextBlock.DataContext property - verify if it is set to your instance of the Foo type. The Binding appears to be correct. Commented May 26, 2010 at 4:11
  • I have added the exact case that i am dealing with Commented May 26, 2010 at 7:05

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Terror it's definitely a FAQ when it comes to WPF binding. It needs to be more visibly documented somehow.
0

Try This

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

Hope this will work.. Good Luck !

1 Comment

Thanks for your comment :) i already tried that, i was confused when it didnt work so i thought something more sinister was at work. a more complete example is available for your perusal :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.