if the parent’s datasource has a child property that is a collection (let’s say it is called ChildCollection) is there a trick to reference it?
So, this code sample is basically what I am attempting to do. But when I use this approach I do not get any data to my child controls.
<UserControl>
<UserControl.Resources>
<sample:Data x:Key="MyData" />
</UserControl.Resources>
<Canvas DataContext="{StaticResource MyData}">
<TextBlock Text="{Binding Title}" />
<My:UserControl DataContext="{Binding ChildCollection}" />
</Canvas>
</UserControl>
My dependency property looks like this:
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(IEnumerable), typeof(ButtonList),
new UIPropertyMetadata(new PropertyChangedCallback(DataChanged)));
public DoubleCollection Data
{
get { return (DoubleCollection)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
static void DataChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as FrameworkElement).DataContext = e.NewValue;
}
public void SetData(IEnumerable data)
{
(View as CollectionViewSource).Source = data;
}
Thank you in advance for your help.
If I understand your code right, you want the collection to be in the UserControl’s DataProperty.
To achieve this, you have to do the Binding like this:
Instead of:
Hope this is helpful. Also: I dont know if a canvas inherits it’s DataContext to childs. Use a Panel instead (Grid/Stackpanel/WrapPanel).
Jan