我想在每个项目之间的 ListView 中添加一条水平线(参见图片)。
我不确定如何调整当前的 xaml 来做到这一点。如果可能的话,我希望线条在图片的两端淡出。
谢谢。
当前的 XAML:
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="#787f82" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
BorderBrush="Transparent"
BorderThickness="0"
Background="{TemplateBinding Background}">
<GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
示例图片:
最佳答案
我自己就能解决这个问题。我将在下面发布我的代码,以便其他人有机会自己完成。
我只是为每个项目的底部设置了一个边框,并对边框应用了渐变。
<Style x:Key="level1" TargetType="{x:Type ListViewItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="#787f82" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
BorderThickness="0,0,0,2"
Background="{TemplateBinding Background}">
<GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0" Opacity="0.3">
<GradientStop Offset="0" Color="transparent"/>
<GradientStop Offset="0.5" Color="white"/>
<GradientStop Offset="1" Color="transparent"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="images/selection-large.png"/>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="images/selection-large.png"/>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="FontWeight" Value="bold" />
</Trigger>
</Style.Triggers>
</Style>
关于WPF XAML ListView 水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27445305/