利用Silverlight DataGrid LoadingRow事件传入参数DataGridRowEventArgs

我们可以获取到Row对象She之其背景。

下面是一个简单示例

C#

XAMl:

 
  
  1. <UserControl x:Class="SilverlightApplication1.MainPage" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     mc:Ignorable="d" 
  7.              xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"   
  8.     d:DesignHeight="300" d:DesignWidth="400" 
  9.              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
  10.      xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"     
  11.              xmlns:Test="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1">  
  12.     <Grid x:Name="LayoutRoot">  
  13.         <Grid.RowDefinitions>  
  14.             <RowDefinition ></RowDefinition>  
  15.             <RowDefinition Height="26" ></RowDefinition>  
  16.         </Grid.RowDefinitions>  
  17.         <data:DataGrid Grid.Row="0" x:Name="dgSource" Margin="3,3,3,3" RowBackground="Black" AutoGenerateColumns="False" LoadingRow="dgSource_LoadingRow" >  
  18.             <data:DataGrid.Columns>  
  19.                 <data:DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True" />  
  20.                 <data:DataGridTextColumn Header="Name" Binding="{Binding Name,Mode=TwoWay}" />  
  21.                 <data:DataGridTextColumn Header="Group" Binding="{Binding Group,Mode=TwoWay}" />  
  22.                   
  23.                 <data:DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True" />  
  24.                 <data:DataGridTextColumn Header="Name" Binding="{Binding Name,Mode=TwoWay}" />  
  25.                 <data:DataGridTextColumn Header="Group" Binding="{Binding Group,Mode=TwoWay}" />  
  26.                   
  27.                 <data:DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True" />  
  28.                 <data:DataGridTextColumn Header="Name" Binding="{Binding Name,Mode=TwoWay}" />  
  29.                 <data:DataGridTextColumn Header="Group" Binding="{Binding Group,Mode=TwoWay}" />  
  30.             </data:DataGrid.Columns>              
  31.         </data:DataGrid>  
  32.         <StackPanel Orientation="Horizontal" Grid.Row="1">  
  33.             <TextBox x:Name="maxCount" Width="300" Text="100000"></TextBox>  
  34.             <Button x:Name="loadSource1" Click="LoadSource_Click" Content="Load Source" />  
  35.             <Button x:Name="test1" Click="test1_Click" Content="Test bind" />  
  36.             <Button  x:Name="test2"  Click="test2_Click" Content="Test 2"/>  
  37.         </StackPanel>  
  38.     </Grid>  
  39. </UserControl>  
  40.    
 
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12.  
  13. namespace SilverlightApplication1  
  14. {  
  15.     public partial class MainPage : UserControl  
  16.     {  
  17.         public MainPage()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.  
  22.         List<SourceModel> gridSources = new List<SourceModel>();  
  23.  
  24.         private void UserControl_Loaded(object sender, RoutedEventArgs e)  
  25.         {  
  26.         }  
  27.  
  28.         private void LoadSource(int n)  
  29.         {  
  30.             if (n < 1)  
  31.                 throw new Exception("should >=1");  
  32.             gridSources.Clear();  
  33.             for (int i = 0; i < n; i++)  
  34.             {  
  35.                 gridSources.Add(new SourceModel() { ID = i.ToString(), Name = "test" + i, Group = ((int)(new Random().NextDouble() * i)).ToString() });  
  36.             }  
  37.  
  38.             int j = 0;  
  39.             gridSources = gridSources.OrderBy(t => t.Group).ToList();  
  40.             gridSources.GroupBy(t => t.Group).ToList().ForEach(t =>  
  41.             {  
  42.                 t.ToList().ForEach(k => k.BG = brushs[j % brushs.Length]);  
  43.                 j++;  
  44.             });  
  45.         }  
  46.         Brush[] brushs = new Brush[] { new SolidColorBrush(Color.FromArgb(128, 135, 206, 235)), new SolidColorBrush(Color.FromArgb(255, 240, 255, 240)), new SolidColorBrush(Color.FromArgb(140, 255, 97, 0)) };  
  47.         private void test1_Click(object sender, RoutedEventArgs e)  
  48.         {  
  49.             dgSource.ItemsSource = gridSources;  
  50.         }  
  51.  
  52.         private void test2_Click(object sender, RoutedEventArgs e)  
  53.         {  
  54.  
  55.         }  
  56.  
  57.         private void LoadSource_Click(object sender, RoutedEventArgs e)  
  58.         {  
  59.             LoadSource(int.Parse(this.maxCount.Text));  
  60.         }  
  61.  
  62.         private void dgSource_LoadingRow(object sender, DataGridRowEventArgs e)  
  63.         {  
  64.             e.Row.Background = (e.Row.DataContext as SourceModel).BG;  
  65.         }  
  66.     }  
  67.  
  68.     public class SourceModel  
  69.     {  
  70.         public string ID { get; set; }  
  71.         public string Name { get; set; }  
  72.         public string Group { get; set; }  
  73.         public Brush BG { get; set; }  
  74.     }  
  75.