My app looks like this:
SectionHeader
SectionHeader
Content
SectionHeader
Content
SectionHeader is a user control with two dependency properties = Title and Apps.
Title does not change but Apps needs to be bound to the main window view model Apps property. The Apps property is only required for two of the three section headers.
<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
Apps="{Binding Path=Apps}" />
This is how it is at the moment. The problem is that the Apps don’t show up.
In SectionHeader the DataContext is set to itself as follows which allows the Title to show up.
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Apps is the ItemsSource for a ItemsControl in the UserControl:
<ItemsControl
ItemsSource="{Binding Apps}">
So my questions are:
- How can I data bind to a UserControl DP?
- Is their an easier way of doing this layout without UserControls?
EDIT:
Forgot to mention that Apps is an ObservableCollection of AppsItems.
This is what my DP looks like:
public static readonly DependencyProperty AppsProperty = DependencyProperty.Register("Apps",
typeof (ObservableCollection<AppsItem>), typeof (SectionHeader),
new PropertyMetadata(null, OnAppsPropertyChanged));
private static void OnAppsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine("Hello!!!");
var sectionHeader = d as SectionHeader;
var value = e.NewValue as ObservableCollection<AppsItem>;
sectionHeader.Apps = value;
}
Give a name to your usecontrol and try binding like this
and root is the name give to your usercontrol
it is element binding to user control which has Apps property