ItemsControl ItemTemplate Binding

Posted by Wonko the Sane on Stack Overflow See other posts from Stack Overflow or by Wonko the Sane
Published on 2010-06-17T15:21:35Z Indexed on 2010/06/17 16:43 UTC
Read the original article Hit count: 431

Filed under:
|
|
|

Hi All,

In WPF4.0, I have a class that contains other class types as properties (combining multiple data types for display). Something like:

public partial class Owner
{
     public string OwnerName { get; set; }
     public int    OwnerId   { get; set; }
}

partial class ForDisplay
{
    public Owner OwnerData { get; set; }
    public int Credit { get; set; }
}

In my window, I have an ItemsControl with the following (clipped for clarity):

<ItemsControl ItemsSource={Binding}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <local:MyDisplayControl 
                OwnerName={Binding OwnerData.OwnerName}
                Credit={Binding Credit} />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I then get a collection of display information from the data layer, and set the DataContext of the ItemsControl to this collection. The "Credit" property gets displayed correctly, but the OwnerName property does not. Instead, I get a binding error:

Error 40: BindingExpression path error: 'OwnerName' property not found on 'object' ''ForDisplay' (HashCode=449124874)'. BindingExpression:Path=OwnerName; DataItem='ForDisplay' (HashCode=449124874); target element is 'TextBlock' (Name=txtOwnerName'); target property is 'Text' (type 'String')

I don't understand why this is attempting to look for the OwnerName property in the ForDisplay class, rather than in the Owner class from the ForDisplay OwnerData property.

Edit It appears that it has something to do with using the custom control. If I bind the same properties to a TextBlock, they work correctly.

<ItemsControl ItemsSource={Binding}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <StackPanel>
              <local:MyDisplayControl 
                        OwnerName={Binding OwnerData.OwnerName}
                        Credit={Binding Credit} />
              <TextBlock Text="{Binding OwnerData.OwnerName}" />
              <TextBlock Text="{Binding Credit}" />
          </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Thanks, wTs

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding