检查Trigger属性XAML中的值范围

时间:2015-12-29 10:54:45

标签: c# wpf 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>

如果TextBlock包含值“1”,上面的代码将Background和Foreground属性设置为'LightGreen',是否可以在触发器中检查一系列intiger值,例如,如果textblock值是1-10,那么背景和前景属性需要更改为LightGreen。

2 个答案:

答案 0 :(得分:2)

试试此代码

转换器

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

答案 1 :(得分:-1)