带有图像的 WPF 按钮触发鼠标悬停以更改图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14275755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:58:40  来源:igfitidea点击:

WPF Button with image Trigger Mouseover to change Image

wpfbuttontriggersmouseoverstyling

提问by Manuel H?dl

i've a Button like this

我有一个这样的按钮

    <Button Content="Add">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type Button}">
                                    <StackPanel Orientation="Horizontal">
                                    <Image x:Name="image" Source="../Resources/Images/Icons/Pruefplan_Add_32_gray.png" Margin="8" />
                                        <TextBlock  x:Name="text" Text="ADD" Margin="3,0,3,0" VerticalAlignment="Center"></TextBlock>
                                    </StackPanel>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="true">
                                            <Setter TargetName="image" Property="Source" Value="../Resources/Images/Icons/Pruefplan_Add_32.png" />
                                            <Setter TargetName="text" Property="Opacity" Value=".5" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Button.Style>
            </Button>

This Button has a Styling and a Trigger to change the Image on MouseOver. It works all fine. But i want to use a global Resource for the Button.

这个按钮有一个样式和一个触发器来改变鼠标悬停时的图像。它工作正常。但我想为按钮使用全局资源。

    <Style x:Key="ButtonsMenue1" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#02FFFFFF" />

    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">

        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Grid Background="{TemplateBinding Background}">

                    <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                              Margin="10"
                                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                              RecognizesAccessKey="True"
                                                              TextBlock.FontFamily="Segoe WP Light"
                                                              TextBlock.Foreground="Black"
                                                              TextBlock.FontSize="14"

                                                              />

                    <Rectangle x:Name="border" 
                                                           SnapsToDevicePixels="True"
                                                           IsHitTestVisible="False" 
                                                           Opacity="0.25"
                                                           Width="10" Height="20" VerticalAlignment="Stretch" HorizontalAlignment="Right" 
                                                           Fill="DarkGray" Margin="15,0,0,0" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="contentPresenter" Property="Opacity" Value="1" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter TargetName="contentPresenter" Property="Opacity" Value=".5" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="true" />
                    <Trigger Property="ToggleButton.IsChecked" Value="true" />
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                        <Setter TargetName="border" Property="Rectangle.Width" Value="2"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

I know i can use

我知道我可以使用

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonsMenue1}">

But it wont show for example my Rectangle as Separator. And it looks not good. Do you have a nice Solution for me ? nice Wishes

但它不会将例如我的矩形显示为分隔符。而且看起来不太好。你有一个很好的解决方案吗?美好的愿望

Manuel

曼努埃尔

回答by MatthiasG

Unfortunately you cannot inherit a ControlTemplate. The template you define for the inherited style will override the template you defined for the base style.

不幸的是,您不能继承ControlTemplate. 您为继承样式定义的模板将覆盖您为基本样式定义的模板。

If you want to use a base template and then want to modify it you will have to find a way to do it with properties. You would than have to define a custom control which has a DependencyProperty to handle special requirements. Something like:

如果您想使用基本模板,然后想对其进行修改,则必须找到一种使用属性的方法。您将不得不定义一个具有 DependencyProperty 的自定义控件来处理特殊要求。就像是:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonsMenue1}">
    <Setter Property="ImageForMouseOver" Value="..." />
</Style>