WPF-转换器(DataGrid根据单元格内容改变颜色)
以下是学习笔记:
https://www.bilibili.com/video/BV1gq4y1D76d?p=55&spm_id_from=pageDriver&vd_source=3f21d2e208ef0bf2c49a9be7560735e5
1,转换器的作用:将值的类型转换另外一种类型
例子说明:当IsGorwing值为true,IsGorwing就变向上箭头绿色显示;否则向下箭头,红色提示。
2,定义转换器
/// <summary> /// 转换器(布尔到箭头的转换) /// </summary> public class BoolToArrowConverter : IValueConverter//需要继承IValueConverter { /// <summary> /// modle向view转换 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && bool.Parse(value.ToString()))//如果为true,转换向上的箭头 { return "↑"; } else { return "↓"; } } /// <summary> /// view向modle转换 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
/// <summary> /// 转换器(布尔到箭头的转换) /// </summary> public class BoolToArrowConverter : IValueConverter//需要继承IValueConverter { /// <summary> /// modle向view转换 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && bool.Parse(value.ToString()))//如果为true,转换向上的箭头 { return "↑"; } else { return "↓"; } } /// <summary> /// view向modle转换 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
3,使用转换器
【3.1】引用转换器所在的命名空间
xmlns:converter_suibian="clr-namespace:Zhaoxi.CourseManagement.Converter"
【3.2】在xaml文件中进行资源声明
<UserControl.Resources> <converter_suibian:BoolToArrowConverter x:Key="BoolToArrowConverter_suibian"/> <converter_suibian:BoolToBrushConverter x:Key="BoolToBrushConverter_suibian"/> </UserControl.Resources>
【3.3】绑定属性,添加转换器
<TextBlock Text="{Binding IsGrowing ,Converter={StaticResource BoolToArrowConverter_suibian}}" Foreground="{Binding IsGrowing,Converter={StaticResource BoolToBrushConverter_suibian}}" Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0"/> <TextBlock Text="{Binding ChangeRate}" Grid.Column="1" Foreground="{Binding IsGrowing,Converter={StaticResource BoolToBrushConverter_suibian}}" Grid.Row="1" VerticalAlignment="Center" Margin="10,0,0,0"/>
DataGrid根据单元格内容改变颜色
1,在项目中新建Converter文件夹,定义转换器
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using System.Windows.Media; namespace ToolsDataCollection.Converter { [ValueConversion(typeof(string), typeof(string))] public class StringToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string strValue = value.ToString().ToUpper(); if (strValue == "OK") { return null; } else { return Brushes.Red; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return ""; } } }
2,
xmlns:colorConverter="clr-namespace:ToolsDataCollection.Converter"
<colorConverter:StringToColorConverter x:Key="DataGridColorConverter"/>
<!--<DataGridTextColumn Width="*" Header="状态" Binding="{Binding TighteningStatus}" CellStyle="{StaticResource DataGridCellStyle}>--> <DataGridTemplateColumn Width="*" Header="状态" CellStyle="{StaticResource DataGridCellStyle}"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding TighteningStatus}" Background="{Binding Path=TighteningStatus,Converter={StaticResource DataGridColorConverter}}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>