Vishnu Babu
Vishnu Babu

Reputation: 1275

Checking Range of values in Trigger property XAML

  <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
              <Trigger Property="Text" Value="1">
              <Setter Property="Background" Value="LightGreen"/>
              <Setter Property="Foreground" Value="LightGreen"/>   
             </Style.Triggers>
        </Style>                                         
 </DataGridTextColumn.ElementStyle>

The code above sets the Background and Foreground property to 'LightGreen' if the TextBlock contains the value "1", is it possible to check a range of intiger values in trigger for example if the textblock value is from 1 - 10 then the Background and Foreground property need to change to LightGreen.

Upvotes: 1

Views: 2127

Answers (2)

Justin CI
Justin CI

Reputation: 2741

Try this code

Converter

/// <summary>
/// The actual implementation of InverseBooleanVisibilityConverter
/// </summary>
internal class RangeConverter : IValueConverter
{
    /// <summary>
    /// Converters the Boolean value to Visibility inversely
    /// </summary>
    /// <param name="value">The Boolean value</param>
    /// <param name="targetType">The target value</param>
    /// <param name="parameter">The parameter</param>
    /// <param name="culture">The culture of the value</param>
    /// <returns>Returns the a Visibility Type Value</returns>
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int j;
        Int32.TryParse(value as string, out j);
        if (j <= 10)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Converters the Visibility value to Boolean inversely
    /// </summary>
    /// <param name="value">The Boolean value</param>
    /// <param name="targetType">The target value</param>
    /// <param name="parameter">The parameter</param>
    /// <param name="culture">The culture of the value</param>
    /// <returns>Returns the a Visibility Type Value</returns>
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML

<Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cef="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Window1" x:Name="testWindow" Height="500" Loaded="Window_Loaded" Width="300" >

    <Window.Resources>
        <cef:RangeConverter x:Key="rangeConv"></cef:RangeConverter>
    </Window.Resources>
    <Grid >
        <StackPanel x:Name="stk">
            <TextBlock Text="1" Width="100">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=rangeConv}}" Value="True">
                                <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </StackPanel>
    </Grid>
</Window>

Upvotes: 2

Joseph
Joseph

Reputation: 1074

You should use a custom converter to achieve this please read the below article

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8ad8c14-95aa-4ed4-b806-d0ae874a8d26/conditional-triggers?forum=wpf

Upvotes: -1

Related Questions