【问题标题】:ListBox items separator when more than one item多个项目时 ListBox 项目分隔符
【发布时间】: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>

我尝试在结束标记之前添加分隔符 &lt;Separator Margin="30 0 30 0" BorderBrush="#CCCCCC" BorderThickness="2"/&gt; 但它会使应用程序崩溃。我应该以某种方式将其放入 DataTemplate 中吗?我认为它会显示在每个项目下(即使只有一个项目)。

【问题讨论】:

  • 尝试使用DataTrigger,绑定previousData,设置FallBackValue
  • 或者你只想在第一个项目下显示一个分隔符,那里有一个以上的项目?
  • 只有一项时是否要隐藏分隔符only?当有两个或更多项目时,如果在最后一个项目下方有一个分隔符可以吗?
  • 我希望将所有项目分开,即分隔符应该只在项目之间。如果只有一项,则没有分隔符。

标签: wpf xaml


【解决方案1】:

我想,你想要实现这样的目标:

<DataTemplate x:Key="TypeOneViewModel" DataType="{x:Type vm:TypeOneViewModel}">
    <DockPanel LastChildFill="True">
        <Separator x:Name="separator" HorizontalAlignment="Stretch" VerticalAlignment="Top" DockPanel.Dock="Top">
            <Separator.Style>
                <Style TargetType="Separator">
                    <Setter Property="Visibility" Value="Visible"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}, FallbackValue={x:Null}}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Separator.Style>
        </Separator>
        <!--Rest of your DataTemplate-->
        <notifications:TypeOneNotification Focusable="False" MinHeight="30" Background="Beige"/>
    </DockPanel>
</DataTemplate>

这将隐藏分隔符,如果项目没有PreviousData,那么对于第一个项目。

如果多次使用,您可以轻松地将 Separator 样式带到外部,并将其称为 StaticResource。

【讨论】:

  • 但是我应该在哪里添加这段代码? (对不起,我还是 xaml 的初学者)
  • @ValCool 在那里,您在 x:Key TypeOneViewModel 和 TypeTwo 上都有 DataTemplates 声明
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多