• WPF学习笔记-DataGrid后台动态生成列,设置列样式并使用值转换器


    一、后台动态生成DataGrid的列

    1 HistoricalDataDG.Columns.Clear();
    2 HistoricalDataDG.Columns.Add(new DataGridTextColumn() { Header = "时间", ElementStyle = (System.Windows.Style)FindResource("TbrqStyle"), IsReadOnly = true });
    3 HistoricalDataDG.Columns.Add(new DataGridTextColumn() { Header = "T01(℃)", Binding = new Binding("T01"), ElementStyle = (System.Windows.Style)FindResource("dgCell_T01"), IsReadOnly = true});

    在后台使用前台xaml里的style  使用FindResource()

    二、设置列样式并使用值转换器

    前台设置的样式代码

    <UserControl.Resources>
            <ResourceDictionary>
    <root:DGColorConverter x:Key="DGColorConverter"/>
    <Style x:Key="TbrqStyle" TargetType="TextBlock"> <Setter Property="Text" Value="{Binding Path=Tbrq,StringFormat='yyyy-MM-dd HH:mm:ss'}"> </Setter> </Style> <Style x:Key="dgCell_T01" TargetType="TextBlock"> <!-- 文本居中--> <Setter Property="TextAlignment" Value="Center"/> <!-- 根据其他列的值设置文本颜色--> <Setter Property="Foreground"> <Setter.Value> <MultiBinding Converter="{StaticResource DGColorConverter}"> <Binding Path="T01"></Binding> <Binding Path="High_Temperature"></Binding> <Binding Path="Mim_Temperature"></Binding> </MultiBinding> </Setter.Value> </Setter> </Style> </ResourceDictionary> </UserControl.Resources>

    TbrqStyle设置时间显示格式为24小时制,在实际使用时发现后台不能设置Binding,否则显示格式无效

    值转换器DGColorConverter

     1 public class DGColorConverter : IMultiValueConverter
     2 {
     3         /// 需传入一组对象,(基础值 比对值)
     4         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     5         {
     6             if (values[0] == DependencyProperty.UnsetValue)
     7             {
     8                 return "";
     9             }
    10                 
    11             float tvalue = float.Parse(values[0].ToString());
    12             float hvalue = float.Parse(values[1].ToString());
    13             float mvalue = float.Parse(values[2].ToString());
    14             if (tvalue > hvalue || tvalue < mvalue)
    15             {
    16                 return new SolidColorBrush(Colors.Red);
    17             }
    18             else
    19             {
    20                 return new SolidColorBrush(Colors.Black);
    21             }
    22         }
    23 
    24         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    25         {
    26             throw new NotImplementedException();
    27         }
    28 }

    通过与其他列的值进行比较,使当前列的文字显示不同颜色

    参考

    https://www.cnblogs.com/jameslif/archive/2013/07/24/3209955.html

    https://www.cnblogs.com/woodenmancool/p/4268539.html?utm_source=tuicool&utm_medium=referral

    http://www.cnblogs.com/lingboxingzi/archive/2012/05/09/2491478.html

  • 相关阅读:
    使用CuteFTP登陆FTP(servU)服务器后无法LIST目录和文件的解决方法
    delphi技巧集锦之一
    delphi技巧集锦之二
    关于varchar(max), nvarchar(max)和varbinary(max)
    别告诉我你会用WORD
    Showmodal与show的区别
    SET ANSI_NULLS ON的用法
    {右键我的电脑无法打开计算机管理}解决方法
    Word常用技巧
    Excel 使用技巧集锦——163种技巧
  • 原文地址:https://www.cnblogs.com/gxsxc/p/10132687.html
Copyright © 2020-2023  润新知