WPF-带有GridView的ListView样式

ListView是展示数据的常用控件,这里简单对带有GridView的ListView样式进行设置。

 <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="HeadStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="OverridesDefaultStyle" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <Grid Name="g" Background="LightBlue">
                            <Border Name="bd" Padding="{TemplateBinding Padding}">
                                <ContentPresenter Margin="5,4,1,3" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                            </Border>
                        </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 Offset="0.3" Color="DeepPink" />
                                            <GradientStop Offset="1" Color="White" />
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!--  ListViewItem的样式  -->
        <Style x:Key="listitem1" 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"
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="True">
                                <GridViewRowPresenter
                                    Margin="3"
                                    Columns="{TemplateBinding GridView.ColumnCollection}"
                                    Content="{TemplateBinding Content}" />
                            </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 Offset="0.31" Color="LightGray" />
                                                    <GradientStop Offset="0.88" Color="Gray" />
                                                </LinearGradientBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger.Setters>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>

此样式设置了表头样式,和表的每一行样式。

 <Grid>
        <ListView
            x:Name="lvUsers"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
           >
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource HeadStyle}">
                    <GridViewColumn
                        Width="120"
                        DisplayMemberBinding="{Binding Name}"
                        Header="Name" />
                    <GridViewColumn
                        Width="50"
                        DisplayMemberBinding="{Binding Age}"
                        Header="Age" />
                    <GridViewColumn
                        Width="150"
                        DisplayMemberBinding="{Binding Mail}"
                        Header="Mail" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

 

.cs文件

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
            items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" });
            lvUsers.ItemsSource = items;
        }
    }
    public class User
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string Mail { get; set; }
    }

效果图:

 

posted @ 2019-11-21 14:35  MrKing&  阅读(4186)  评论(0编辑  收藏  举报