继续聊WPF——为ListView的行设置样式

 
关键点:要设置ListView的行样式,设置Listview的ItemContainerStyle属性,但在本例中,由于我把样式应用于所有的ListViewItem,故不用设置该属性。
 
<Window x:Class="Wpf_GridHeaderStyle_sample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:Wpf_GridHeaderStyle_sample"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ControlTemplate x:Key="tmpForThumb" TargetType="{x:Type Thumb}">
            <Border>
                <Rectangle Width="1" Name="rec">
                    <Rectangle.Fill>
                        <LinearGradientBrush StartPoint="0.5,0.3" EndPoint="1,1">
                            <GradientStop Color="Orange" Offset="0.22"/>
                            <GradientStop Color="White" Offset="1"/>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="rec" Property="Fill" Value="LightGreen"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <Grid Name="g">
                            <Grid.Background>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <GradientStop Color="Gold" Offset="0.3"/>
                                    <GradientStop Color="LightYellow" Offset="1"/>
                                </LinearGradientBrush>
                            </Grid.Background>
                            <Border Name="bd" Padding="{TemplateBinding Padding}">
                                <ContentPresenter Margin="5,4,1,3"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                            </Border>
                            <Thumb Name="PART_HeaderGripper" Template="{StaticResource tmpForThumb}" HorizontalAlignment="Right" Margin="0,0,-1,0"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="g" Property="Background">
                                    <Setter.Value>
                                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                            <GradientStop Color="DeepPink" Offset="0.3"/>
                                            <GradientStop Color="White" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!--数据源-->
        <c:Students x:Key="DBSource"/>
        <!--ListViewItem的样式-->
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Setters>
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                                                    <Border x:Name="bd"
                                BorderThickness="1"
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="True">
                            <GridViewRowPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  Margin="3"/>
                        </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Trigger.Setters>
                                        <Setter TargetName="bd" Property="Background">
                                            <Setter.Value>
                                                <LinearGradientBrush StartPoint="0.5,0.22" EndPoint="0.5,1">
                                                    <GradientStop Color="LightGray" Offset="0.31"/>
                                                    <GradientStop Color="Gray" Offset="0.88"/>
                                                </LinearGradientBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger.Setters>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView x:Name="lv" ItemsSource="{StaticResource DBSource}">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="姓名" Width="110"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Phone}" Header="联系电话" Width="125"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

继续聊WPF——为ListView的行设置样式
 
继续聊WPF——为ListView的行设置样式

原文链接: http://blog.csdn.net/tcjiaan/article/details/6984761

你可能感兴趣的:(继续聊WPF——为ListView的行设置样式)