zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    webpack高级概念,Dllplugin打包性能配置(系列十八)
    webpack高级概念,resolve配置(配置文件格式以及文件路径变量)(系列十七)
    webpack高级概念,eslint配置(系列十六)
    webpack高级概念,解决单页面应用路由问题(vue用脚手架,404找不到页面二)(系列十五)
    webpack高级概念,webpack-dev-server解决单页面应用路由问题(手动搭建webpack,不是用脚手架,404找不到页面,一)(系列十五)
    webpack高级概念,使用 WebpackDevServer 实现请求转发二(系列十四)
    webpack高级概念,使用 WebpackDevServer 实现请求转发一 (系列十四)
    webpack高级概念,typeScript的打包配置(系列十三)
    DevEco Device Tool 2.1 Beta1 的Hi3861在Windows平台的编译体验
    最全HarmonyOS文档和社区资源使用技巧
  • 原文地址:https://www.cnblogs.com/BABLOVE/p/3741458.html
Copyright © 2011-2022 走看看