zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    洛谷模板汇总
    BZOJ1787【AHOI2008】Meet紧急集合 <LCA>
    HDU3068 最长回文 <Manacher>
    UVa12345 Dynamic len(set(a[L:R])) <带修莫队>
    BZOJ2038 小Z的袜子 <莫队>
    BZOJ1103【POI2007】大都市meg <树上差分+树状数组>
    BZOJ3226【SDOI2008】校门外的区间
    BZOJ1012【JSOI2008】最大数 <线段树>
    20170918~24周总结
    BZOJ1934【SHOI2007】善意的投票 <网络流>
  • 原文地址:https://www.cnblogs.com/gxsxc/p/10132687.html
Copyright © 2011-2022 走看看