Set WPF cell style using converter

I have two styles of WPF styles, and I want to apply them based on the converter. I am my example below. I am trying to change the background color (in a real application, I will change more than that, but this is not a question, so I just simplify it).

<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>

Then I have a converter:

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();
    }       
}

How can I then call this in a cell style?

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" />

Maybe the answer is that this is not possible, and I need to go down a different route?

+4
source share
1 answer

Binding does not work in a CellStyleDataGridColumn, so try creating your own style for the TextBoxtarget type instead DataGridCelland write DataGridTemplateColumnas follows:

<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:

<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, CellStyle :

<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>
+1

Source: https://habr.com/ru/post/1531351/


All Articles