我想根據單元格中包含的值更改單元格的文本顏色 我使用值轉換器,但不知何故傳遞給Convert函數的對象的類型是DataRowView,我想通過這個單元格,因爲我想根據它的值一次點亮一個單元格。希望這是有道理的。wpf爲DataGrid中的單元格着色基於值
謝謝!
代碼如果我申請到DataGrid的風格:
<UserControl.Resources>
<local:MyBkColorConverter x:Key="bkColorCvrt"/>
<Style x:Key="GridStyle" TargetType="DataGrid">
<Setter Property="ItemsSource" Value="{Binding}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="RowBackground" Value="Transparent" />
<Setter Property="HeadersVisibility" Value="None" />
<Setter Property="GridLinesVisibility" Value="None" />
<Setter Property="SelectionUnit" Value="Cell" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground">
<Setter.Value>
<Binding Converter="{StaticResource bkColorCvrt}"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Black">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
和C#部分:
公共類MyBkColorConverter:的IValueConverter { #地區的IValueConverter成員
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//The type of value here is actually DataRowView
//here i would like to have a cell passed. is that possible to archive?
return Brushes.LightGray;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
我的網格綁定到DataTable,所以我沒有自己定義任何列。我在哪裏粘貼此代碼? – Anya 2012-04-05 09:59:12
如果您知道哪些列正在聯機到您的源行,那麼您可以將它與上面的相同。例如你有員工datatable和名稱是列,那麼上述將工作正常。 – JSJ 2012-04-05 10:53:35