如何在鼠标悬停时更改按钮样式并使用多重绑定?

时间:2015-11-05 18:06:31

标签: wpf button mouseover multibinding

我在Button.Style中使用MultiBinding来根据布尔值(IsScenSelBtnEnabled)设置Style值。

<Button.Style>
  <MultiBinding Converter="{StaticResource StyleConverter}">
    <Binding Path="IsScenSelBtnEnabled" />
    <Binding Source="{StaticResource BlueButton2}" />
    <Binding Source="{StaticResource DisabledButton2}" />
  </MultiBinding>
</Button.Style>

当按钮上发生鼠标悬停事件时,我还需要将Style值更改为名为BlueButton3的样式。我花了几个小时来搜索这个并尝试弄清楚它我自己,但我没有运气。我看到可以使用Style.Triggers来在触发发生时设置Style属性(例如IsMouseOver),但是我无法使用当前的MultiBinding。

<Style.Triggers>
  <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="LightGreen"/>
  </Trigger>
</Style.Triggers>

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为反过来回答这个问题是最好的。您在IsMouseOver的样式触发器中有正确的想法。但是,由于WPF对DependencyProperty的评估顺序,它无法正常工作:

https://msdn.microsoft.com/en-us/library/vstudio/ms743230(v=vs.100).aspx#listing

如您所见,首先评估样式触发器,然后接下来评估模板触发器。因此,您的样式触发器不会在视觉上浮出水面,因为按钮的默认模板已经触发了IsMouseOver。

为了让按钮背景变为绿色,您需要覆盖ControlTemplate:

    <Button.Template>
                    <ControlTemplate TargetType="{x:Type ButtonBase}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Button.IsDefaulted" Value="True">
                                <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="border" Value="LightGreen"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="LightGreen"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="True">
                                <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
    </Button.Template>

注意&#34; IsMouseOver&#34;触发器将背景设置为lightgreen。

关于问题的第一部分......就我个人而言,我认为在不使用转换和多重绑定的情况下,解决问题的方法更简单。由于您必须已经覆盖按钮的controltemplate,您可以将按钮的默认属性设置为您想要的任何内容,然后添加到新控件模板&#34; IsEnabled&#34;触发部分,用于设置禁用按钮时所需的属性。你的最终xaml看起来像这样:

 <Button Background="Blue" IsEnabled="{Binding IsScenSelBtnEnabled}">A Button!
            <Button.Template>
                <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsDefaulted" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="border" Value="LightGreen"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="LightGreen"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                            <!--Insert any other setters for your disabled style here!-->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>