2015-02-24 25 views
0

我想通过从数据库中获取值来更改数据网格的行背景颜色。 我有2个值“错误”和“确定”。如果列字符串值为ERROR,那么行颜色将为红色,如果OK,则它必须为绿色。该值通过执行查询从数据库中获取。我在数据集中有这些值。我不清楚如何实现这一目标?如何使用wpf中的数据库中的值更改数据网格行背景颜色

我曾尝试下面的代码:

<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> 

视图模型:

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 
} 

简单的类

public class myItem 
{ 
    public string Status { get; set; } 
    public string Name { get; set; } 
} 

Dispatcher类

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); 
     } 
    } 
} 

转换器:

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字符串,则背景行颜色变为红色,反之亦然。

+0

您的代码似乎工作。我不确定我是否了解您遇到问题的位置。 – 2015-02-24 07:36:23

+0

我没有得到如何将数据集绑定到viewmodel – Dipika 2015-02-24 07:37:36

+0

数据集是System.Data.DataSet类的一个实例吗? – 2015-02-24 07:39:43

回答

0

首先Collection属性应该以这种方式来声明:

public DataSet Collection { ... } 

ItemSource在XAML结合也应改变:

ItemsSource="{Binding Path=Collection.Tables[Table1], Mode=OneWay,... 

在上述表1的代码是一个表的名称在你想要绑定的数据集中。以这种方式来测试此代码创建一个简单的数据集:

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); 
} 
2

我得到了我的答案......这样stuffs..Just没有neew使用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); 
} 
+0

在这种情况下,“e”是什么? – ajivani 2016-08-30 18:12:12

+0

@ajivani'LoadingRow'是DataGrid的事件。如果我没有弄错的话,Dipika正在编写上述代码。 – Gopi 2016-10-25 17:16:04

相关问题