【发布时间】:2017-09-06 16:30:28
【问题描述】:
我有一个 ListBox,我试图在项目之间插入一个分隔符,但我希望在有多个项目时显示分隔符。
<ListBox Grid.Row="1" x:Name="WarningListBox" Visibility="Visible"
HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Path=Warnings}">
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="Height" Value="0"/>
<Style.Triggers>
<DataTrigger Binding="{Binding CanShowAll}" Value="True">
<Setter Property="Height" Value="Auto"/>
<Setter Property="MaxHeight" Value="120"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource NewDesignListItemBoxStyle}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Resources>
<DataTemplate x:Key="TypeOneViewModel" DataType="{x:Type vm:TypeOneViewModel}">
<notifications:TypeOneNotification Focusable="False" MinHeight="30" Background="Beige"/>
</DataTemplate>
<DataTemplate x:Key="TypeTwoViewModel" DataType="{x:Type vm:TypeTwoViewModel}">
<notifications:TypeTwoNotification Focusable="False" MinHeight="30" Background="Beige"/>
</DataTemplate>
</ListBox.Resources>
<ListBox.ItemTemplateSelector>
<templateSelectors:WarningTemplateSelector
TypeOneDataTemplate="{StaticResource TypeOneViewModel}"
TypeTwoDataTemplate="{StaticResource TypeTwoViewModel}"/>
</ListBox.ItemTemplateSelector>
</ListBox>
我尝试在结束标记之前添加分隔符
<Separator Margin="30 0 30 0" BorderBrush="#CCCCCC" BorderThickness="2"/>
但它会使应用程序崩溃。我应该以某种方式将其放入 DataTemplate 中吗?我认为它会显示在每个项目下(即使只有一个项目)。
【问题讨论】:
-
尝试使用DataTrigger,绑定previousData,设置FallBackValue
-
或者你只想在第一个项目下显示一个分隔符,那里有一个以上的项目?
-
只有一项时是否要隐藏分隔符only?当有两个或更多项目时,如果在最后一个项目下方有一个分隔符可以吗?
-
我希望将所有项目分开,即分隔符应该只在项目之间。如果只有一项,则没有分隔符。