Herioz
Herioz

Reputation: 111

ItemControl DataTemplate with UIElement list

I'm trying to develop library of user controls that arranges multiple UIElements in specific way. I use ItemControl to show list of UIElements. I want to surround every item from item control with Stack. I would like to use my library more or less this manner.

  <pcLayouts:ListLayout>

      <pcLayouts:ListLayout.ParentItems>
          <TextBlock Width="145">1</TextBlock>
          <TextBlock>2</TextBlock>
          <TextBlock>3</TextBlock>
      </pcLayouts:ListLayout.ParentItems>

   </pcLayouts:ListLayout>


I declared dependency property in backing class ListLayout cs and xaml files.

        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>

It seems DataTemplate isn't used at all when binding to Binding ParentItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}. How can I use this data template or is there another way?

Upvotes: 0

Views: 219

Answers (1)

Jannik Botteram
Jannik Botteram

Reputation: 46

this is because ItemsControl.IsItemItsOwnContainerOverride returns true for UIElement. Normally a ContentPresenter is used which generates the DataTemplate.

If you insist on using DataTemplate you create a new class derived from ItemsControl and override IsItemItsOwnContainerOverride to return false.

Upvotes: 1

Related Questions