带有 UI 元素列表的 ItemsControl DataTemplate
ItemControl DataTemplate with UIElement list
我正在尝试开发以特定方式排列多个 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>
我在支持 class 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。我如何使用这个数据模板或者有其他方法吗?
这是因为 ItemsControl.IsItemItsOwnContainerOverride
returns true
for UIElement
。通常使用 ContentPresenter 来生成 DataTemplate。
如果您坚持使用 DataTemplate,您可以创建一个从 ItemsControl 派生的新 class 并将 IsItemItsOwnContainerOverride
覆盖为 return false。
我正在尝试开发以特定方式排列多个 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>
我在支持 class 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。我如何使用这个数据模板或者有其他方法吗?
这是因为 ItemsControl.IsItemItsOwnContainerOverride
returns true
for UIElement
。通常使用 ContentPresenter 来生成 DataTemplate。
如果您坚持使用 DataTemplate,您可以创建一个从 ItemsControl 派生的新 class 并将 IsItemItsOwnContainerOverride
覆盖为 return false。