具有UIElement列表的ItemControl DataTemplate

我正在尝试开发以特定方式排列多个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。如何使用此数据模板,或者还有其他方法吗?

cengyicang 回答:具有UIElement列表的ItemControl DataTemplate

这是因为ItemsControl.IsItemItsOwnContainerOverridetrue返回了UIElement。通常,使用ContentPresenter来生成DataTemplate。

如果您坚持使用DataTemplate,则可以创建一个从ItemsControl派生的新类,并覆盖IsItemItsOwnContainerOverride以返回false。

本文链接:https://www.f2er.com/2637082.html

大家都在问