简体   繁体   中英

Converter not calling when using in IsMouseOver trigger

I want to make button background of which will become darker or lighter accroding to parameters. So i make this style:

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

And I have converter:

 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;
    }

When i click on button everything goes fine. But when mouse over event is firing button starts to blink and converter isn't calling. How to make it work? And i use this style on button:

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

add this

TargetName="Border" 

to the IsMouseOver trigger and it works.

Second the first Trigger element is not closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM