绑定到ItemsControl的ObservableCollection中的项目的不同行高,具体取决于数据类型

时间:2019-02-14 10:32:23

标签: c# wpf xaml itemscontrol itemspaneltemplate

有人知道是否有可能根据所讨论项目的数据类型来改变ItemsControl中每个项目的高度?

比方说,我有一个ObservableCollection对象,大致像这样:

SomeItemToDraw
SomeItemToDraw
SomeItemToDraw
Divider
SomeItemToDraw
SomeItemToDraw
Divider
SomeItemToDraw

我已将其绑定到ItemsControl,每种类型具有不同的数据模板:

<ItemsControl ItemsSource="{Binding Myitems}" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <UniformGrid Columns="1" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.Resources>

    <DataTemplate DataType="{x:Type example:SomeItemToDraw>
      <!-- A big ascii owl -->
    </DataTemplate>
    <DataTemplate DataType="{x:Type example:Divider>
      <!-- A single line -->
    </DataTemplate>

</ItemsControl.Resources>

很明显,在上文中,每一项都被分配了相等的空间。因此,单行被赋予与大型ascii猫头鹰相同的空间。

有可能在没有固定集合的情况下(每次都是随机的)来确保我们仍然填充父容器,并且确保'Divider'数据类型的高度仅为1px,而其他的猫头鹰则各占一半?

或者这不可能吗?在哪种情况下,我们要看一个DataGrid,将样式的每一位都设置为完全不可见?

0 个答案:

没有答案
相关问题