silverlight中DataGrid数据高亮显示
效果如图所示,
<UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightGridData.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:thislocal="clr-namespace:SilverlightGridData.Util" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> <UserControl.Resources> <thislocal:ColorConvert x:Key="ConverColor"></thislocal:ColorConvert> </UserControl.Resources> <Grid x:Name="LayoutRoot" > <controls:TabControl Height="350" Width="500"> <controls:TabItem Name="选项一" Header="选项卡"> <StackPanel> <data:DataGrid FrozenColumnCount="2" HorizontalAlignment="Center" VerticalContentAlignment="Center" Height="280" Width="480" Name="Datagrid1" Margin="0,10,0,0" AutoGenerateColumns="False"> <data:DataGrid.Columns > <data:DataGridTemplateColumn Header="姓名" > <data:DataGridTemplateColumn.CellTemplate > <DataTemplate > <TextBlock Text="{Binding Name}" Width ="100" Foreground="{Binding Path=Name,Converter={StaticResource ConverColor}}"> </TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="年龄" > <data:DataGridTemplateColumn.CellTemplate > <DataTemplate > <TextBlock Text="{Binding Age}" Canvas.Top="100" Canvas.Left="100" Foreground="{Binding Path=Age,Converter={StaticResource ConverColor}}"> <Run Foreground="Red">as</Run> </TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="学校" > <data:DataGridTemplateColumn.CellTemplate > <DataTemplate > <TextBlock Text="{Binding School}" Canvas.Top="100" Canvas.Left="100" Foreground="{Binding Path=School,Converter={StaticResource ConverColor}}"> <Run Foreground="Red">as</Run> </TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="年级" > <data:DataGridTemplateColumn.CellTemplate > <DataTemplate > <TextBlock Text="{Binding Grade}" Canvas.Top="100" Canvas.Left="100" Foreground="{Binding Path=Grade,Converter={StaticResource ConverColor}}"> <Run Foreground="Red">as</Run> </TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="班级" > <data:DataGridTemplateColumn.CellTemplate > <DataTemplate > <TextBlock Text="{Binding Class}" Canvas.Top="100" Canvas.Left="100" Foreground="{Binding Path=Class,Converter={StaticResource ConverColor}}"> <Run Foreground="Red">as</Run> </TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid> </StackPanel> </controls:TabItem> </controls:TabControl> </Grid> </UserControl>
public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { list = new List<Student>(); list.Add(new Student { Name = "张三", Age = 6, School = "北京第一小学", Grade = "一年级", Class = "A2" }); list.Add(new Student { Name = "赵龙", Age = 7, School = "北京第二小学", Grade = "二年级", Class = "A1" }); list.Add(new Student { Name = "李锐", Age = 8, School = "北京第三小学", Grade = "六年级", Class = "A2" }); list.Add(new Student { Name = "宋敏", Age = 9, School = "北京第四小学", Grade = "三年级", Class = "A3" }); list.Add(new Student { Name = "吴晓", Age = 10, School = "北京第五小学", Grade = "四年级", Class = "A4" }); list.Add(new Student { Name = "张刚", Age = 11, School = "北京第六小学", Grade = "五年级", Class = "A5" }); list.Add(new Student { Name = "李刚", Age = 12, School = "北京第七小学", Grade = "六年级", Class = "A6" }); this.Datagrid1.ItemsSource = list; } public class Student { public string Name { get; set; } public int Age { get; set; } public string School { get; set; } public string Grade { get; set; } public string Class { get; set; } }
类文件中实现IValueConverter接口
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // 根据文字显示颜色 if (value.Equals("张三")) { return "Red"; } if (value.Equals("北京第五小学")) { return "Red"; } if (value.Equals("A5")) { return "Red"; } else { return "Black"; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; }