silverlight中DataGrid数据高亮显示
具体如下
xaml如下<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>
一个类文件中实现IValueConverter接口
public class ColorConvert:IValueConverter { #region 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; }