2015-12-29 3 views
0
<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> 

Код выше устанавливает фона и переднего плана свойство «LIGHTGREEN», если TextBlock содержит значение «1», можно проверить диапазон значений intiger в триггером для Например, если значение текстового блока составляет от 1 до 10, то свойство Background и Foreground необходимо изменить на LightGreen.Проверка диапазона значений в Trigger собственности XAML

ответ

1

Попробуйте этот код

конвертер

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