Change WPF Button Foreground when the “IsMouseOver” is True

会有一股神秘感。 提交于 2020-01-17 06:06:24

问题


I have seen a few similar questions but not of the solutions seem to work.

I would prefer to have the solution through XAML but wouldn't mind it through code (VB.net) either.

I have this code currently, not sure how much of it is correct:

<Style TargetType="{x:Type Button}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Foreground" Value="DarkSlateGray"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="White">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="DodgerBlue"/>
                </Trigger>
            </Style.Triggers>
        </Style>

Thanks in advance.


回答1:


It is generally correct. But do note that your ContentPresenter in your template has no Content property defined. Of course, if you need more complicated triggers, you can also combine with other things like IsEnabled into a MultiTrigger.



来源:https://stackoverflow.com/questions/37786603/change-wpf-button-foreground-when-the-ismouseover-is-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!