我正在尝试开发以特定方式排列多个UIElement的用户控件库。我使用ItemControl来显示UIElement的列表。我想用Stack包围项目控件中的每个项目。 我想或多或少以这种方式使用我的图书馆。
<pcLayouts:ListLayout>
<pcLayouts:ListLayout.ParentItems>
<TextBlock Width="145">1</TextBlock>
<TextBlock>2</TextBlock>
<TextBlock>3</TextBlock>
</pcLayouts:ListLayout.ParentItems>
</pcLayouts:ListLayout>
我在支持类ListLayout cs和xaml文件中声明了依赖项属性。
public static readonly DependencyProperty ParentItemsProperty = DependencyProperty.Register(
"ParentItems",typeof(ObservableCollection<UIElement>),typeof(ColumnLayout),new PropertyMetadata(new ObservableCollection<UIElement>()));
...
public ObservableCollection<UIElement> ParentItems
{
get { return (ObservableCollection<UIElement>)Getvalue(ParentItemsProperty); }
set
{
Setvalue(ParentItemsProperty,value);
OnPropertyChanged();
}
}
<StackPanel x:Name="MainPanel" Orientation="Vertical">
<ItemsControl ItemsSource="{Binding ParentItems,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}" >
<ItemsControl.ItemsPanel>
<itemspaneltemplate>
<WrapPanel />
</itemspaneltemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WHAT SHOULD I PUT HERE??/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
似乎绑定到Binding ParentItems,AncestorType={x:Type UserControl}}
时根本不使用DataTemplate。如何使用此数据模板,或者还有其他方法吗?