C#WPF RibbonButton在MouseOver上更改图标

时间:2017-10-13 08:20:38

标签: c# wpf mouseover ribbon

我有一个Ribbonbutton,我想更改MouseOver上的Icon但它似乎不起作用。

这是我的代码:

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

它只显示第一个Icon&#34; disconnect.png&#34;鼠标悬停在所有其他按钮上,但没有图像更改。

我也用这种方式尝试过,使用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>

这里根本没有显示图标。

1 个答案:

答案 0 :(得分:1)

找到了答案!

WPF RibbonButton: LargeImageSource and Label not updated via DataTriggers

  

问题是您在按钮本身中设置LargeImageSource和Label的属性。当你这样做时,它会占用你的风格触发器。我建议在样式中使用setter来设置默认值,并在属性设置中删除按钮。

所以必须是:

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

从Button本身移除“LargeImageSource”。