在IsMouseOver触发器中使用时,转换器未调用

时间:2014-08-08 14:08:11

标签: c# wpf

我想让按钮背景变得更暗或更轻,这取决于参数。所以我做这个风格:

 <Style x:Key="ButtonFlatStyle" TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="{StaticResource ButtonFlatForegroundBrush}" />
    <Setter Property="FontSize" Value="{StaticResource CommonFontSize}" />
    <Setter Property="FontFamily" Value="{StaticResource CommonFontFamily}" />
    <Setter Property="Padding" Value="10,5,10,5" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="Border"
                        Margin="0"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter Margin="{TemplateBinding Padding}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="True" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background">
                            <Setter.Value>
                                <Binding Converter="{StaticResource BleachBrushConverter}"
                                         Path="Background"
                                         RelativeSource="{RelativeSource TemplatedParent}">
                                    <Binding.ConverterParameter>
                                        <system:Double>-0.5</system:Double>
                                    </Binding.ConverterParameter>
                                </Binding>
                            </Setter.Value>
                        </Setter>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <Binding Converter="{StaticResource BleachBrushConverter}"
                                         Path="Background"
                                         RelativeSource="{RelativeSource TemplatedParent}">
                                    <Binding.ConverterParameter>
                                        <system:Double>0.5</system:Double>
                                    </Binding.ConverterParameter>
                                </Binding>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我有转换器:

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var brush = (SolidColorBrush) value;
        if (brush == null) return null;
        var brightnessFactor = float.Parse(parameter.ToString());
        var originalColour = brush.Color;
        var bleachOrDarkness = brightnessFactor > 0 ? Color.FromRgb(255, 255, 255) : Color.FromRgb(0, 0, 0);
        var adjustedColour = Lerp(originalColour, bleachOrDarkness, Math.Abs(brightnessFactor));
        var newBrush = new SolidColorBrush(adjustedColour);
        return newBrush;
    }

当我点击按钮时,一切都很顺利。但是,当鼠标悬停在事件上时,按钮开始闪烁,转换器不会调用。如何使它工作? 我在按钮上使用这种风格:

<Style x:Key="ButonFlatStyleGray"
       BasedOn="{StaticResource ButtonFlatStyle}"
       TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource ButtonGreyBackgroundBrush}" />
</Style>

1 个答案:

答案 0 :(得分:0)

添加此

TargetName="Border" 

到IsMouseOver触发器,它可以工作。

其次,第一个Trigger元素未关闭。