我的应用程序长这样:
SectionHeader
SectionHeader
Content
SectionHeader
Content
SectionHeader
是一个用户控件,具有两个依赖属性:Title 和 Apps。
Title 不会改变,但是 Apps 需要绑定到主窗口视图模型的 Apps 属性。Apps 属性仅适用于三个部分标题中的两个。
<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
Apps="{Binding Path=Apps}" />
目前状况如下。问题在于应用程序没有显示出来。
在 SectionHeader
中,DataContext 被设置为自身,以便使标题显示出来。
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Apps是UserControl中ItemsControl
的ItemsSource:
<ItemsControl
ItemsSource="{Binding Apps}">
所以我的问题是:
- 如何将数据绑定到用户控件 DP?
- 是否有不使用用户控件的更简单的方法来完成此布局?
编辑:
忘记提到 Apps 是 AppsItems 的 ObservableCollection。
这是我的 DP 内容:
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;
}