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

主要写一些最近一段时间学习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.       
  22.       
  23.     <Style  TargetType="DataGridRow">  
  24.         <Setter Property="Background" Value="#F2F2F2" />  
  25.         <Setter Property="Height" Value="25"/>  
  26.         <Setter Property="Foreground" Value="Black" />  
  27.         <Style.Triggers>  
  28.               
  29.             <Trigger Property="AlternationIndex" Value="0" >  
  30.                 <Setter Property="Background" Value="#e7e7e7" />  
  31.             Trigger>  
  32.             <Trigger Property="AlternationIndex" Value="1" >  
  33.                 <Setter Property="Background" Value="#f2f2f2" />  
  34.             Trigger>  
  35.   
  36.             <Trigger Property="IsMouseOver" Value="True">  
  37.                 <Setter Property="Background" Value="LightGray"/>  
  38.                   
  39.             Trigger>  
  40.   
  41.             <Trigger Property="IsSelected" Value="True">  
  42.                 <Setter Property="Foreground" Value="Black"/>  
  43.             Trigger>  
  44.         Style.Triggers>  
  45.     Style>  
  46.   
  47.       
  48.     <Style TargetType="DataGridCell">  
  49.         <Setter Property="Template">  
  50.             <Setter.Value>  
  51.                 <ControlTemplate TargetType="DataGridCell">  
  52.                     <TextBlock TextAlignment="Center" VerticalAlignment="Center"  >  
  53.                            <ContentPresenter />  
  54.                     TextBlock>  
  55.                 ControlTemplate>  
  56.             Setter.Value>  
  57.         Setter>  
  58.         <Style.Triggers>  
  59.             <Trigger Property="IsSelected" Value="True">  
  60.                 

你可能感兴趣的:(WPF)