【发布时间】:2020-03-16 17:05:52
【问题描述】:
我正在尝试开发以特定方式排列多个 UIElement 的用户控件库。我使用 ItemControl 来显示 UIElements 列表。我想用 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, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}} 时似乎根本没有使用DataTemplate。我该如何使用这个数据模板还是有其他方法?
【问题讨论】:
标签: wpf itemscontrol