C# WPF RibbonButton 在 MouseOver 上更改图标

C# WPF RibbonButton Change Icon on MouseOver

我有一个功能区按钮,我想更改 MouseOver 上的图标,但它似乎不起作用。

这是我的代码:

<RibbonButton Label="Verbindung testen" LargeImageSource="../Resources/Buttons/disconnect.png" Command="{Binding SettingsVM.TestConnectionCommand}">
                    <RibbonButton.Style>
                        <Style TargetType="{x:Type RibbonButton}">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="LargeImageSource" Value="../Resources/Buttons/connect.png"/>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="False">
                                    <Setter Property="LargeImageSource" Value="../Resources/Buttons/disconnect.png"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </RibbonButton.Style>
</RibbonButton>

它只显示第一个图标 "disconnect.png",当鼠标悬停在它上面时,它会像所有其他按钮一样突出显示,但没有图像变化。

我也这样试过,用ControlTemplate:

<RibbonButton Label="Verbindung testen" LargeImageSource="../Resources/Buttons/disconnect.png" Command="{Binding SettingsVM.TestConnectionCommand}">
<RibbonButton.Template>
    <ControlTemplate TargetType="{x:Type RibbonButton}">
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="LargeImageSource" Value="../Resources/Buttons/connect.png"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="LargeImageSource" Value="../Resources/Buttons/disconnect.png"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</RibbonButton.Template>

这里根本不显示图标。

找到答案了!

WPF RibbonButton: LargeImageSource and Label not updated via DataTriggers

The problem is your setting properties for LargeImageSource and Label in the button itself. When you do this it takes precidence over your style triggers. I suggest using setters in the style to set your defaults, and remove the property settings it the button.

所以它必须是:

<RibbonButton Label="Verbindung testen" Command="{Binding SettingsVM.TestConnectionCommand}">
<RibbonButton.Style>
    <Style TargetType="{x:Type RibbonButton}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="LargeImageSource" Value="../Resources/Buttons/connect.png"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="LargeImageSource" Value="../Resources/Buttons/disconnect.png"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</RibbonButton.Style>

从按钮本身中删除 "LargeImageSource"。