使用转换器设置WPF单元格样式

时间:2014-03-12 14:22:06

标签: c# wpf xaml datagrid

我有两个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" />

也许答案是不可能的,我需要走另一条路?

1 个答案:

答案 0 :(得分:3)

绑定不能在DataGridColumn的CellStyle中工作,因此请尝试为TextBox目标类型而不是DataGridCell创建样式并编写DataGridTemplateColumn这样的<DataGrid.Resources> <local:AmountToCellStyleConverter x:Key="StyleConverter" /> </DataGrid.Resources> ... <DataGridTemplateColumn Width="1.5*" Header="SimpleHeader" IsReadOnly="False"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Style="{Binding Path=NumberValue, Converter={StaticResource StyleConverter}}" Text="{Binding Path=NumberValue}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>

String

或者写一个值而不是两个,在这种情况下,Converter的输入值的类型将是<DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Style="{Binding Path=Text, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource StyleConverter}}" Text="{Binding Path=NumberValue}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate>

TextBox

App.xaml的样式位于<Application.Resources> <Style x:Key="WinCellStyle" TargetType="{x:Type TextBox}"> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBoxBase}"> <Border Background="LightGreen" BorderThickness="1"> <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="LossCellStyle" TargetType="{x:Type TextBox}"> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border Background="LightSalmon" BorderThickness="1"> <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Application.Resources>

Converter

public class AmountToCellStyleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var valueAsDecimal = (int)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, CultureInfo culture) { throw new NotImplementedException(); } } 看起来像这样:

True

或者你可以这样做:改变转换器的逻辑以返回False<DataGridTextColumn Header="SimpleHeader" Width="1.5*" Binding="{Binding Path=NumberValue}"> <DataGridTextColumn.CellStyle> <Style TargetType="{x:Type DataGridCell}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=NumberValue, Converter={StaticResource MyConverter}}" Value="True"> <Setter Property="Background" Value="Yellow" /> <Setter Property="BorderThickness" Value="1" /> </DataTrigger> <DataTrigger Binding="{Binding Path=NumberValue, Converter={StaticResource MyConverter}}" Value="False"> <Setter Property="Background" Value="Red" /> <Setter Property="BorderThickness" Value="1" /> </DataTrigger> </Style.Triggers> </Style> </DataGridTextColumn.CellStyle> </DataGridTextColumn> ,在CellStyle中你可以这样写:

{{1}}