如何使用从 wpf 中的数据库获取的值更改数据网格行背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28689920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change datagrid row background color with value taken from database in wpf
提问by Dipika
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 be red and if OK then It must be Green. This value get from database by executing query. I have these values in dataset. I am not clear that how to achieve this?
我想通过从数据库中获取值来更改数据网格行背景颜色。我有 2 个值“错误”和“正常”。如果列字符串值为错误,则行颜色将为红色,如果正常,则必须为绿色。该值通过执行查询从数据库中获取。我在数据集中有这些值。我不清楚如何实现这一目标?
I have 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:
视图模型:
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();
}
}
I just want to bind dataset column to trigger. If trigger get ERROR string then background row color change to red and vice versa.
我只想绑定数据集列来触发。如果触发器获取 ERROR 字符串,则背景行颜色变为红色,反之亦然。
采纳答案by Micha? Komorowski
Firstly Collectionproperty should be declared in this way:
首先Collection属性应该以这种方式声明:
public DataSet Collection { ... }
ItemSourcebinding in XAML should be also changed:
ItemSourceXAML 中的绑定也应该改变:
ItemsSource="{Binding Path=Collection.Tables[Table1], Mode=OneWay,...
In the code above Table1is a name of a table within a dataset you want to bind to. To test this code create a simple dataset in this way:
在上面的代码中,Table1是要绑定到的数据集中的表的名称。要测试此代码,请以这种方式创建一个简单的数据集:
public viewmodel()
{
var tb = new DataTable("Table1");
tb.Columns.Add("Status");
tb.Columns.Add("Name");
tb.Rows.Add("OK", "name1");
tb.Rows.Add("ERROR", "name2");
tb.Rows.Add("ERROR", "name3");
Collection2 = new DataSet();
Collection2.Tables.Add(tb);
}
回答by Dipika
I got My answer...No neew of such stuffs..Just used loadingrow event. This is very much usefull. See Below
我得到了我的答案......没有这样的东西......只是使用了loadingrow事件。这非常有用。见下文
DataGridRow row = e.Row;
DataRowView rView = row.Item as DataRowView
if(rView != null && rView.Row.ItemArray[4].ToString().Contains("ERROR"))
{
e.row.Background= new SolidColorBrush(Color.Red);
}
else
{
e.row.Background= new SolidColorBrush(Color.Green);
}

