0
votes

Look at this example:

I've created a custom control with a collection as a dependency property and filled the items of the collection with subitems getting the values from a binding. If I create the subitems with fixed values everything works, if I bind their values I get binding errors.

This is the user control with the readonly dependency property:

public partial class UserControl1 : UserControl
{
    private static DependencyPropertyKey TextsPropertyKey = DependencyProperty.RegisterReadOnly("Texts", typeof(ItemCollection), typeof(UserControl1), new FrameworkPropertyMetadata(new ItemCollection()));
    public static DependencyProperty TextsProperty = TextsPropertyKey.DependencyProperty;

    public ItemCollection Texts
    {
        get
        {
            return (ItemCollection)GetValue(TextsProperty);
        }
        set
        {
            SetValue(TextsProperty, value);
        }
    }

    public UserControl1()
    {
        ItemCollection texts = new ItemCollection();
        SetValue(TextsPropertyKey, texts);
        InitializeComponent();
    }
}

This is the Window XAML:

<Window x:Class="ControlList.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ControlList="clr-namespace:ControlList"
Title="Window1" Height="300" Width="300">
<Grid>
    <ControlList:UserControl1>
        <ControlList:UserControl1.Texts>
            <ControlList:ItemOfTheList Text="{Binding Text1}"></ControlList:ItemOfTheList>
            <ControlList:ItemOfTheList Text="{Binding Text2}"></ControlList:ItemOfTheList>
        </ControlList:UserControl1.Texts>
    </ControlList:UserControl1>
</Grid>
</Window>

The ItemOfTheList class is just an object with a string Dependency property:

public class ItemOfTheList : DependencyObject
{
    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ItemOfTheList));

    public string Text
    {
        get
        {
            return (string)GetValue(TextProperty);
        }
        set
        {
            SetValue(TextProperty, value);
        }
    }

    public override string ToString()
    {
        return this.Text;
    }
}

And the item collection is just a non generic FreezableCollection:

public class ItemCollection : FreezableCollection<ItemOfTheList>
{
}

In this way I get the following errors:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text1; DataItem=null; target element is 'ItemOfTheList' (HashCode=52697953); target property is 'Text' (type 'String') System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text2; DataItem=null; target element is 'ItemOfTheList' (HashCode=22597652); target property is 'Text' (type 'String')

If I change ItemOfTheList to a FrameworkElement I always get at the output window that the DataContext is null. How can I inherit the datacontext of the UserControl in the ItemOfTheList objects?

1

1 Answers

1
votes

Your child items are not part of the logical tree so cannot participate in bindings. Please see my blog post here for details on how to rectify this.