【发布时间】:2014-03-12 14:22:06
【问题描述】:
我有两种 WPF 单元格样式,我想基于转换器应用它们。 我是下面的示例,我正在尝试更改背景颜色(在实际应用中我会更改更多,但这不是问题的重点,所以我只是在简化)。
<Style TargetType="{x:Type DataGridCell}" x:Key="WinCellStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="LightGreen"
BorderBrush="Transparent"
BorderThickness="1"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type DataGridCell}" x:Key="LossCellStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="LightSalmon"
BorderBrush="Transparent"
BorderThickness="1"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后我有一个转换器:
public class AmountToCellStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var valueAsDecimal = (decimal?) value;
if (valueAsDecimal > 0)
{
return Application.Current.FindResource("WinCellStyle") as Style;
}
return Application.Current.FindResource("LossCellStyle") as Style;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后如何在单元格样式中调用它?
XAML:
<Window.Resources>
<converter:AmountToCellStyleConverter x:Key="AmountToCellStyleConverter"/>
</Window.Resources>
...
<DataGridTextColumn CellStyle="{Binding ??? What goes here}" Binding="{Binding Path=MarketBookSelection.TotalWagerStakeWin, StringFormat=N2}" Header="Stake Win" Width="Auto" />
也许答案是这是不可能的,我需要走其他路线?
【问题讨论】: