当值正确时,触发器不起作用

时间:2012-02-03 12:49:03

标签: .net wpf datatrigger

当值为Above24时,触发器不起作用。我有一个文本框来测试转换器,它可以工作。但是这个标签的触发器不起作用。 转换器低于XAML代码

<Label x:Name="label" Content="{Binding Text, ElementName=textBox2}"HorizontalAlignment="Left" Margin="69,110,0,0" Width="122.63" VerticalAlignment="Top"Background="White"FontSize="26.667">

           <Style TargetType="Label">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Label.Content, Converter={StaticResource dc}}"   Value="Above24">
                        <Setter  Property="Foreground">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="Black" Offset="1"/>
                                    <GradientStop Color="#FFCE0A0A"/>
                                    <GradientStop Color="#FF2A0202" Offset="0.75"/>
                                    <GradientStop Color="#FF5B0404" Offset="0.5"/>
                                    <GradientStop Color="#FF9C0707" Offset="0.25"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
class 
DataConverter
:
IValueConverter
{
        object IValueConverter.Convert
(object value, 
Type targetType,
 object parameter, 
System.Globalization.CultureInfo culture)
{
            if (value == null) return false;
            if (string.IsNullOrEmpty(value.ToString())) return false;
            if (Convert.ToInt32(value) < 18) return "Below18";
            if (Convert.ToInt32(value) > 24) return "Above24";
            return Convert.ToInt32(value) < 100;
        }
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试,注意使用RelativeSource。

<Style TargetType="Label"> 
            <Style.Triggers> 
                <DataTrigger Binding="{Binding Content, RelativeSource={RelativeSource Self},Converter={StaticResource dc}}"   Value="Above24"> 
                    <Setter  Property="Foreground"> 
                        <Setter.Value> 
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
                                <GradientStop Color="Black" Offset="1"/> 
                                <GradientStop Color="#FFCE0A0A"/> 
                                <GradientStop Color="#FF2A0202" Offset="0.75"/> 
                                <GradientStop Color="#FF5B0404" Offset="0.5"/> 
                                <GradientStop Color="#FF9C0707" Offset="0.25"/> 
                            </LinearGradientBrush> 
                        </Setter.Value> 
                    </Setter> 
                </DataTrigger> 
            </Style.Triggers> 
        </Style> 
    </Label.Style> 
</Label>