如何使用从wpf中的数据库获取的值更改datagried行背景颜色 [英] how to change datagried row background color with value taken from database in wpf
问题描述
我想通过从数据库中获取值来更改数据网格行背景颜色。我有2个值ERROR和OK。如果列字符串值为ERROR,则行颜色将为红色。然后好吧那么它必须是绿色的。此值通过触发查询从数据库获取。我在数据集中有这个值。这该怎么做??我尝试下面的代码
I want to change datagrid row background color, by taking value from Database. I have 2 value "ERROR" and "OK". If Column string value is ERROR then row color will b red. and ik OK then It must b Green. This value get from database by firing query. I have this values in dataset. How to do this?? I tried below code
<Window x:Class="stackDatagridColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:stackDatagridColor"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModels:viewmodel x:Key="viewmodel"/>
<viewModels:BrushConverter x:Key="BrushConverter"/>
</Window.Resources>
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding Collection, Mode=TwoWay, Source={StaticResource viewmodel}, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="ERROR">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Status}" Value="OK">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</StackPanel>
</Grid>
Viewmodel
Viewmodel
public class viewmodel : INotifyPropertyChanged
{
private ObservableCollection<myItem> collection;
public ObservableCollection<myItem> Collection
{
get { return collection; }
set { collection = value; OnPropertyChanged("Collection"); }
}
public viewmodel()
{
Collection = new ObservableCollection<myItem>();
myItem item1 = new myItem { Name = "name1", Status = "OK" };
myItem item2 = new myItem { Name = "name2", Status = "ERROR" };
DispatchService.Invoke(() =>
{
Collection.Add(item1);
Collection.Add(item2);
});
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
简单类
Simple Class
public class myItem
{
public string Status { get; set; }
public string Name { get; set; }
}
调度员班级
Dispatcher Class
public static class DispatchService
{
public static void Invoke(Action action)
{
Dispatcher dispatchObject = Application.Current.Dispatcher;
if (dispatchObject == null || dispatchObject.CheckAccess())
{
action();
}
else
{
dispatchObject.Invoke(action);
}
}
}
转换器>
Converter
public class BrushConverter : IValueConverter
{ public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string input = value as string; switch (input) { case "ERROR": return Brushes.Red; case "OK": return Brushes.Green; default: return DependencyProperty.UnsetValue; } }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
我只是想绑定数据集列来触发。如果触发器获得ERROR字符串,则backgrounfrow颜色变为红色。和副vrsa。
I just Want to bind dataset column to trigger. If trigger get ERROR string then backgrounfrow color change to red. and vice vrsa.
推荐答案
这将对你有所帮助:
http://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values [ ^ ]
This will help you:
http://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values[^]
这篇关于如何使用从wpf中的数据库获取的值更改datagried行背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!