2011-04-30 68 views
1

我在datagrid中有一些單元格,我想在特定列中的單元格的值爲0時將其突出顯示爲紅色。我不確定如何處理此問題。基於DataGridCell的值的觸發器

我看過這個問題:WPF: How to highlight all the cells of a DataGrid meeting a condition?但沒有解決方案爲我工作。

在使用樣式觸發器時,觸發器似乎應用於屬性。當我做什麼事情都沒有發生(我假設,因爲內容比簡單的值更多)。

隨着最後建議的解決方案我正在這似乎是一直在爲VS現在,而一個錯誤的表現編譯時的問題:Custom binding class is not working correctly

任何想法如何,我可以做到這一點?

任何人有任何想法?

回答

1

根據DataGridCell的值更改單元格背景顏色的最佳方法是使用Converter爲DataGridTemplateColumn定義DataTemplate,以更改單元格的背景顏色。這裏提供的示例使用MVVM。

的關鍵部分以搜索在下面的例子中包括:

1:XAML,在模型轉換爲顏色的整數(因子):

<TextBlock Text="{Binding Path=FirstName}" 
      Background="{Binding Path=Factor, 
      Converter={StaticResource objectConvter}}" /> 

2:轉換器,它返回一個基於模型的整數屬性的SolidColorBrush:

public class ObjectToBackgroundConverter : IValueConverter 

3:視圖模型,在從按鈕0和1之間的型號改變了整數值點擊觸發一個事件這會改變轉換器中的顏色。

private void OnChangeFactor(object obj) 
{ 
    foreach (var customer in Customers) 
    { 
    if (customer.Factor != 0) 
    { 
     customer.Factor = 0; 
    } 
    else 
    { 
     customer.Factor = 1; 
    } 
    } 
} 

4:型號實現INotifyPropertyChanged用來觸發事件通過調用OnPropertyChanged

private int _factor = 0; 
public int Factor 
{ 
    get { return _factor; } 
    set 
    { 
    _factor = value; 
    OnPropertyChanged("Factor"); 
    } 
} 

我提供了這裏需要我的回答與核心部件使用的以外的所有位來改變背景顏色如 包含ViewModelBase(INotifyPropertyChanged)和DelegateCommand的MVVM模式的基礎,您可以通過谷歌找到它。請注意,我將View的DataContext綁定到代碼隱藏構造函數中的ViewModel。如果需要,我可以發佈這些額外的位。

這裏是XAML:

<Window x:Class="DatagridCellsChangeColor.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Helpers="clr-namespace:DatagridCellsChangeColor.Converter" 
    Title="MainWindow" 
    Height="350" Width="525"> 
    <Window.Resources> 
    <Helpers:ObjectToBackgroundConverter x:Key="objectConvter"/> 
    /Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Button Content="Change Factor" Command="{Binding Path=ChangeFactor}"/> 
    <DataGrid 
    Grid.Row="1" 
    Grid.Column="0" 
    Background="Transparent" 
    ItemsSource="{Binding Customers}" 
    IsReadOnly="True" 
    AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
    <DataGridTemplateColumn 
     Header="First Name" 
     Width="SizeToHeader"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=FirstName}" 
         Background="{Binding Path=Factor, 
         Converter={StaticResource objectConvter}}" /> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTemplateColumn 
     Header="Last Name" 
     Width="SizeToHeader"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBox Text="{Binding Path=LastName}" /> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    </DataGrid.Columns> 
    </DataGrid> 
</Grid> 
</Window> 

這裏是轉換器:

using System; 
using System.Drawing; 
using System.Globalization; 
using System.Windows.Data; 
using System.Windows.Media; 
using Brushes = System.Windows.Media.Brushes; 

namespace DatagridCellsChangeColor.Converter 
{ 
    [ValueConversion(typeof(object), typeof(SolidBrush))] 
    public class ObjectToBackgroundConverter : IValueConverter 
    { 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     int c = (int)value; 

     SolidColorBrush b; 
     if (c == 0) 
     { 
     b = Brushes.Gold; 
     } 
     else 
     { 
     b = Brushes.Green; 
     } 
     return b; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
    } 
} 

這裏是視圖模型:

using System.Collections.ObjectModel; 
using System.Windows.Input; 
using DatagridCellsChangeColor.Commands; 
using DatagridCellsChangeColor.Model; 

namespace DatagridCellsChangeColor.ViewModel 
{ 
    public class MainViewModel : ViewModelBase 
    { 
    public MainViewModel() 
    { 
     ChangeFactor = new DelegateCommand<object>(OnChangeFactor, CanChangeFactor); 
    } 

    private ObservableCollection<Customer> _customers = Customer.GetSampleCustomerList(); 
    public ObservableCollection<Customer> Customers 
    { 
     get 
     { 
     return _customers; 
     } 
    } 

    public ICommand ChangeFactor { get; set; } 
    private void OnChangeFactor(object obj) 
    { 
     foreach (var customer in Customers) 
     { 
     if (customer.Factor != 0) 
     { 
      customer.Factor = 0; 
     } 
     else 
     { 
      customer.Factor = 1; 
     } 
     } 
    } 

    private bool CanChangeFactor(object obj) 
    { 
     return true; 
    } 
    } 
} 

這裏是型號:

using System; 
using System.Collections.ObjectModel; 
using DatagridCellsChangeColor.ViewModel; 

namespace DatagridCellsChangeColor.Model 
{ 
    public class Customer : ViewModelBase 
    { 
    public Customer(String first, string middle, String last, int factor) 
    { 
     this.FirstName = first; 
     this.MiddleName = last; 
     this.LastName = last; 
     this.Factor = factor; 
    } 

    public String FirstName { get; set; } 
    public String MiddleName { get; set; } 
    public String LastName { get; set; } 

    private int _factor = 0; 
    public int Factor 
    { 
     get { return _factor; } 
     set 
     { 
     _factor = value; 
     OnPropertyChanged("Factor"); 
     } 
    } 

    public static ObservableCollection<Customer> GetSampleCustomerList() 
    { 
     return new ObservableCollection<Customer>(new Customer[4] 
           { 
           new Customer("Larry", "A", "Zero", 0), 
           new Customer("Bob", "B", "One", 1), 
           new Customer("Jenny", "C", "Two", 0), 
           new Customer("Lucy", "D", "THree", 2) 
           }); 
    } 
    } 
}