WPF的Datagrid显示,样式(隔行换色)

13 篇文章 0 订阅

原文地址:http://blog.csdn.net/wangweiandsuo/article/details/8500864

主要写一些最近一段时间学习Wpf的显示

直接显示与.net中的gridview有些相似,直接把一个list扔入datagrid中就可以显示,都可以自动的把对象的属性加载到页面上。

MainWindow.xaml.cs

[html]  view plain  copy
  1. public MainWindow()  
  2.        {  
  3.            InitializeComponent();  
  4.        }  
  5.   
  6.        private void Window_Loaded(object sender, RoutedEventArgs e)  
  7.        {  
  8.            List<User> list = new List<User>();  
  9.            for (int i = 0; i < 10; i++)  
  10.            {  
  11.                User u = new User();  
  12.                u.Id = i + 1;  
  13.                u.Name = "aa" + i;  
  14.                list.Add(u);  
  15.            }  
  16.            this.grid_user.ItemsSource = list;  
  17.        }  
  18.    }  
  19.    public class User  
  20.    {  
  21.        public int Id { get; set; }  
  22.        public string Name { get; set; }  
  23.    }  

MainWindow.xaml    AlternationCount="2"隔行换色

[html]  view plain  copy
  1. <Grid>  
  2.         <DataGrid Name="grid_user" IsReadOnly="True" AlternationCount="2">  
  3.             <DataGrid.Columns>  
  4.                 <DataGridTextColumn Header="Id" Width="50" Binding="{Binding Id}"/>  
  5.                 <DataGridTextColumn Header="Name" Width="50" Binding="{Binding Name}"/>  
  6.             </DataGrid.Columns>  
  7.         </DataGrid>  
  8.     </Grid>  


自定义Datagrid的样式

[html]  view plain  copy
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
  3.     <Style TargetType="DataGrid">  
  4.         <!--网格线颜色-->  
  5.         <Setter Property="CanUserResizeColumns" Value="false"/>  
  6.         <Setter Property="Background" Value="#E6DBBB" />  
  7.         <Setter Property="BorderBrush" Value="#d6c79b" />  
  8.         <Setter Property="HorizontalGridLinesBrush">  
  9.             <Setter.Value>  
  10.                 <SolidColorBrush Color="#d6c79b"/>  
  11.             </Setter.Value>  
  12.         </Setter>  
  13.         <Setter Property="VerticalGridLinesBrush">  
  14.             <Setter.Value>  
  15.                 <SolidColorBrush Color="#d6c79b"/>  
  16.             </Setter.Value>  
  17.         </Setter>  
  18.     </Style>  
  19.   
  20.     <!--标题栏样式-->  
  21.     <!--<Style  TargetType="DataGridColumnHeader" >  
  22.         <Setter Property="Width" Value="50"/>  
  23.         <Setter Property="Height" Value="30"/>  
  24.         <Setter Property="FontSize" Value="14" />  
  25.         <Setter Property="Background" Value="White" />  
  26.         <Setter  Property="FontWeight"  Value="Bold"/>  
  27.     </Style>-->  
  28.   
  29.     <Style TargetType="DataGridColumnHeader">  
  30.         <Setter Property="SnapsToDevicePixels" Value="True" />  
  31.         <Setter Property="MinWidth" Value="0" />  
  32.         <Setter Property="MinHeight" Value="28" />  
  33.         <Setter Property="Foreground" Value="#323433" />  
  34.         <Setter Property="FontSize" Value="14" />  
  35.         <Setter Property="Cursor" Value="Hand" />  
  36.         <Setter Property="Template">  
  37.             <Setter.Value>  
  38.                 <ControlTemplate TargetType="DataGridColumnHeader">  
  39.                     <Border x:Name="BackgroundBorder" BorderThickness="0,1,0,1"  
  40.                              BorderBrush="#e6dbba"  
  41.                               Width="Auto">  
  42.                         <Grid >  
  43.                             <Grid.ColumnDefinitions>  
  44.                                 <ColumnDefinition Width="*" />  
  45.                             </Grid.ColumnDefinitions>  
  46.                             <ContentPresenter  Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/>  
  47.                             <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill"  Grid.Column="2" Width="8" Height="6" Fill="White" Margin="0,0,50,0"  
  48.                             VerticalAlignment="Center" RenderTransformOrigin="1,1" />  
  49.                             <Rectangle Width="1" Fill="#d6c79b" HorizontalAlignment="Right" Grid.ColumnSpan="1" />  
  50.                             <!--<TextBlock  Background="Red">  
  51.                             <ContentPresenter></ContentPresenter></TextBlock>-->  
  52.                         </Grid>  
  53.                     </Border>  
  54.                 </ControlTemplate>  
  55.             </Setter.Value>  
  56.         </Setter>  
  57.         <Setter Property="Height" Value="25"/>  
  58.     </Style>  
  59.     <!--行样式触发-->  
  60.     <!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式-->  
  61.     <Style  TargetType="DataGridRow">  
  62.         <Setter Property="Background" Value="#F2F2F2" />  
  63.         <Setter Property="Height" Value="25"/>  
  64.         <Setter Property="Foreground" Value="Black" />  
  65.         <Style.Triggers>  
  66.             <!--隔行换色-->  
  67.             <Trigger Property="AlternationIndex" Value="0" >  
  68.                 <Setter Property="Background" Value="#e7e7e7" />  
  69.             </Trigger>  
  70.             <Trigger Property="AlternationIndex" Value="1" >  
  71.                 <Setter Property="Background" Value="#f2f2f2" />  
  72.             </Trigger>  
  73.   
  74.             <Trigger Property="IsMouseOver" Value="True">  
  75.                 <Setter Property="Background" Value="LightGray"/>  
  76.                 <!--<Setter Property="Foreground" Value="White"/>-->  
  77.             </Trigger>  
  78.   
  79.             <Trigger Property="IsSelected" Value="True">  
  80.                 <Setter Property="Foreground" Value="Black"/>  
  81.             </Trigger>  
  82.         </Style.Triggers>  
  83.     </Style>  
  84.   
  85.     <!--单元格样式触发-->  
  86.     <Style TargetType="DataGridCell">  
  87.         <Setter Property="Template">  
  88.             <Setter.Value>  
  89.                 <ControlTemplate TargetType="DataGridCell">  
  90.                     <TextBlock TextAlignment="Center" VerticalAlignment="Center"  >  
  91.                            <ContentPresenter />  
  92.                     </TextBlock>  
  93.                 </ControlTemplate>  
  94.             </Setter.Value>  
  95.         </Setter>  
  96.         <Style.Triggers>  
  97.             <Trigger Property="IsSelected" Value="True">  
  98.                 <!--<Setter Property="Background" Value="White"/>  
  99.                 <Setter Property="BorderThickness" Value="0"/>-->  
  100.                 <Setter Property="Foreground" Value="Black"/>  
  101.             </Trigger>  
  102.         </Style.Triggers>  
  103.     </Style>  

  1. </ResourceDictionary>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值