如何更改依赖于wpf中的一列的datagried行的backcolor [英] How to change backcolor of datagried row which depend on one column in wpf
问题描述
我有一个dataGrid,其中包含一列红色或绿色图像。如果状态为ERROR或OK,则触发设置红色或绿色图像。如果有红色图像,我只想更改孔行的颜色。
我用XAML代码将Red of Green Image设置如下
< DataGridTemplateColumn >
< DataGridTemplateColumn.CellTemplate >
< DataTemplate >
< span class =code-keyword>< 图片 Name = IsReadImage 来源 = read.png / >
< DataTemplate.Triggers >
< DataTrigger 绑定 = {Binding IsRead} 值 = 错误 >
< Setter TargetName = IsReadImage 属性 = 来源 值 = unread.png / >
< / DataTrigger >
< / DataTemplate.Triggers >
< / DataTemplate >
< / DataGridTemplateColumn.CellTemplate >
< / DataGridTemplateColumn >
我尝试使用XAML代码更改行背景颜色如下所示
< 样式 TargetType = DataGridRow >
< Style .Triggers>
< DataTrigger Binding ={ Binding 有效} 值=错误>
< Setter Property =BackgroundValue =Red/>
< / DataTrigger >
< / Style.Triggers >
< / Style >
但它没有改变背景颜色。请帮帮我
你可以通过一个所谓的价值转换器来实现这个行为:
这个只是一个例子,你可以根据价值设置颜色
。
ValueToColorConverter.cs
< pre lang =cs> 使用系统;
使用 System.Windows.Data;
使用 System.Windows.Media;
命名空间 WpfApplication12
{
class ValueToColorConverter: IValueConverter
{
public object 转换( object value ,键入targetType, object 参数,System.Globalization.CultureInfo culture)
{
if ( value .GetType()== typeof ( bool ))
{
if (( bool ) value )
{
返回 Brushes.Green;
}
其他
{
返回画笔。红色;
}
}
else
{
return value ;
}
}
public object ConvertBack( object value ,输入targetType, object 参数,System.Globalization.CultureInfo文化)
{
throw new NotImplementedException() ;
}
}
}
MainWindow.xaml:
< Window x:Class = WpfApplication12.MainWindow
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local = clr-namespace:WpfApplication12
Title = MainWindow高度= 350宽度= 525 >
< Window.Resources>
< local:ValueToColorConverter x:Key = V2C > < / local:ValueToColorConverter >
< / Window.Resources >
< Grid>
< DataGrid ItemsSource = {Binding Items} AutoGenerateColumns = False >
< DataGrid。柱体和GT;
< DataGridTemplateColumn>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid Background = {Binding IsRead,Converter = {StaticResource V2C}} > < / 网格 >
< / DataTemplate >
< / DataGridTemplateColumn.CellTemplate >
< / DataGridTemplateColumn >
< / DataGrid.Columns >
< / DataGrid >
< / 网格 >
< / 窗口 >
MainWindow.xaml.cs:
using System.Windows;
命名空间 WpfApplication12
{
public partial class MainWindow:Window
{
ViewModel vm;
public MainWindow()
{
this .Loaded + = MainWindow_Loaded;
InitializeComponent();
}
void MainWindow_Loaded( object sender,RoutedEventArgs e)
{
vm = new ViewModel();
this .DataContext = vm;
}
}
}
ViewModel.cs:
使用 System.Collections.ObjectModel;
命名空间 WpfApplication12
{
class ViewModel: ViewModelBase
{
public ViewModel()
{
items = new ObservableCollection< item>();
Items.Add( new Item(){IsRead = true }) ;
Items.Add( new Item(){IsRead = false });
Items.Add( new Item(){IsRead = true });
Items.Add( new Item(){IsRead = false });
Items.Add( new Item(){IsRead = true });
}
private ObservableCollection< item>项目;
public ObservableCollection< item>商品
{
获取
{
返回项;
}
set
{
if (items != value )
{
items = value ;
OnPropertyChanged();
}
}
}
}
}
ViewModelBase。 cs:
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
命名空间 WpfApplication12
{
class ViewModelBase: INotifyPropertyChanged
{
[AttributeUsageAttribute(AttributeTargets.Parameter,Inherited = false )]
public sealed class CallerMemberNameAttribute:Attribute {}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
public void OnPropertyChanged([CallerMemberName] 字符串 propertyName = null )
{
if (PropertyChanged!= null )
{
PropertyChanged( this , new PropertyChangedEventArgs(propertyName));
}
}
}
}
Item.cs:
命名空间 WpfApplication12
{
class 项
{
public bool IsRead {获得跨度>; set ; }
}
}
I have one dataGrid which contain one column for red or green color image. I do trigger to set red or green color image if status is ERROR or OK which gate from database. I just want to change color for hole row if there is red image.
I do XAML code to set Red of Green Image as below
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Name="IsReadImage" Source="read.png"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsRead}" Value="False">
<Setter TargetName="IsReadImage" Property="Source" Value="unread.png"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
And I tried XAML code to change row background color as below
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Active}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
But It did not change background color. Please help me
You can achieve this behaviour by a so-called value converter:
This is just an example of how you could set colours
depending on values.
ValueToColorConverter.cs
using System; using System.Windows.Data; using System.Windows.Media; namespace WpfApplication12 { class ValueToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if(value.GetType() == typeof(bool)) { if((bool)value) { return Brushes.Green; } else { return Brushes.Red; } } else { return value; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
MainWindow.xaml:
<Window x:Class="WpfApplication12.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication12" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:ValueToColorConverter x:Key="V2C"></local:ValueToColorConverter> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid Background="{Binding IsRead,Converter={StaticResource V2C}}"></Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
MainWindow.xaml.cs:
using System.Windows; namespace WpfApplication12 { public partial class MainWindow : Window { ViewModel vm; public MainWindow() { this.Loaded += MainWindow_Loaded; InitializeComponent(); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { vm = new ViewModel(); this.DataContext = vm; } } }
ViewModel.cs:
using System.Collections.ObjectModel; namespace WpfApplication12 { class ViewModel : ViewModelBase { public ViewModel() { items = new ObservableCollection<item>(); Items.Add(new Item() { IsRead = true }); Items.Add(new Item() { IsRead = false }); Items.Add(new Item() { IsRead = true }); Items.Add(new Item() { IsRead = false }); Items.Add(new Item() { IsRead = true }); } private ObservableCollection<item> items; public ObservableCollection<item> Items { get { return items; } set { if (items != value) { items = value; OnPropertyChanged(); } } } } }
ViewModelBase.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApplication12 { class ViewModelBase : INotifyPropertyChanged { [AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)] public sealed class CallerMemberNameAttribute : Attribute { } public event PropertyChangedEventHandler PropertyChanged = delegate { }; public void OnPropertyChanged([CallerMemberName]string propertyName = null) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
Item.cs:
namespace WpfApplication12 { class Item { public bool IsRead { get; set; } } }
这篇关于如何更改依赖于wpf中的一列的datagried行的backcolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!