views:

24

answers:

0

I can't databind to properties I have added to subclasses of UIElement. Can anyone help?

An example: I am displaying a collection of items on a canvas using databinding. I want to include both UIElements and data items (which will have a datatemplate instantiated) in my canvas. The items have properties that determine their position on the canvas (e.g. Left). These are databound to the ItemContainer, as you can see here:

<ItemsControl ItemsSource="{Binding Path=Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left" Value="{Binding Path=Left}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

If the items are data items, the datatemplate is instantiated and the databinding works fine. However, if the items in the list are UIElements (with the appropriate Left property), the databinding fails.

  • I have tried forcing the itemscontrol to put an container (ContentPresenter) around the UIElements, but it makes no difference.
  • I have tried other databinding scenarios, but cannot seem to bind to properties on a UIElement (the scenario presented above is the simplest to understand).