WPF XAML Listview水平线

时间:2014-12-12 14:01:45

标签: wpf xaml listview

我想在每个项目之间的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>

示例图片: enter image description here

2 个答案:

答案 0 :(得分:1)

我能够自己解决这个问题。我将在下面发布我的代码,以便其他人可能有机会自己完成。

我只是为每个项目的底部设置了一个边框,我在边框上应用了一个渐变。

 <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>

答案 1 :(得分:0)

我的解决方案是使用ListView.ItemContainerStyle属性设置项目容器所需的样式

                <ListView ItemsSource="{Binding Champs}" Name="listView1" HorizontalContentAlignment="Stretch" Background="Transparent" Padding="-1" >
                    <ListView.ItemContainerStyle>
                        <Style>
                            <Setter Property="Control.Background" Value="White" />
                            <Setter Property="Control.BorderThickness" Value="0.3" />
                            <Setter Property="Control.BorderBrush" Value="Black" />
                        </Style>
                    </ListView.ItemContainerStyle>

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,5,0,5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Nom}" FontWeight="Bold" FontSize="10" />
                                <TextBox Grid.Row="1" TextAlignment="Center" Text="{Binding Valeur}" />
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListView>