我需要通過代碼來此XAML樣式應用於DataGrid的細胞:應用樣式到DataGridCell編程與轉換器
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Item.Dif, Converter={StaticResource RedValues}}"/>
</Style>
</DataGrid.RowStyle>
到目前爲止,在我的代碼,我可以申請在TextAlignmentProperty一個二傳手,但沒有對前景:
Style style2 = new Style(typeof(DataGridCell));
style2.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));
style2.Setters.Add(new Setter(TextBlock.ForegroundProperty, new RedValues()));
codb = new DataGridTextColumn();
codb.Binding = new Binding("Dif") {
StringFormat = "C",
ConverterCulture = new CultureInfo("pt-PT") };
codb.Header = "Dif";
codb.CellStyle = style2;
grid.Columns.Add(codb);
這是我的轉換器類:
class RedValues: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is decimal) {
decimal quantity = (decimal)value;
if (quantity < 0)
return Brushes.IndianRed;
if (quantity > 0)
return Brushes.ForestGreen;
return Brushes.Black;
}
return Brushes.Yellow;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
你的天堂」在代碼中應用任何轉換器?你的問題到底是什麼? – Muds 2015-03-19 11:42:42
是如何應用轉換器 – celsoap7 2015-03-19 11:43:27
我猜ans應該幫助:) – Muds 2015-03-19 11:49:38