本文介绍了WPF样式按钮鼠标悬停问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个按钮一个简单的鼠标悬停效果,它确实改变了颜色,当鼠标已经结束,但颜色会立即更改为默认按钮背景...我怎么能覆盖这个行为?



这是我的代码:

 风格myBtnStyle =新样式(); 
触发喇嘛=新的触发(){属性= IsMouseOverProperty,值=真};
bla.Setters.Add(新二传手(Control.BackgroundProperty,Brushes.Black));
myBtnStyle.Triggers.Add(BLA);
button2.Style = myBtnStyle;


解决方案

据的,即看中动画建成并删除它,你'重新将需要重写控件模板按钮。幸运的是,这是不是太辛苦。我用源材料想出了以下风格,让你的想法。



<风格X:键=MouseOverButtonStyle的TargetType =按钮>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType =按钮>
< ControlTemplate.Resources>
<风格X:键=ShadowStyle>
< setter属性=Control.ForegroundVALUE =浅灰色/>
< /样式和GT;
< /ControlTemplate.Resources>
< BORDER NAME =边界了borderThickness =1填充=4,2BorderBrush =深灰CornerRadius =3背景={TemplateBinding背景}>
<网格和GT;
< ContentPresenter的Horizo​​ntalAlignment =中心VerticalAlignment =中心NAME =contentShadow风格={StaticResource的ShadowStyle}>
< ContentPresenter.RenderTransform>
< TranslateTransform X =1.0Y =1.0/>
< /ContentPresenter.RenderTransform>
< / ContentPresenter>
< ContentPresenter的Horizo​​ntalAlignment =中心VerticalAlignment =中心NAME =内容/>
< /网格和GT;
< /边框>
< /控件模板>
< /Setter.Value>
< /二传手>
< Style.Triggers>
<触发属性=IsMouseOverVALUE =真>
< setter属性=背景VALUE =米色/>
< /触发>
< /Style.Triggers>
< /样式和GT;



更新:
。如果你在申请死心塌地在风格中的代码,你不希望使用 ResourceDictionary中(可能是更好的方式来做到这一点),你可以加载风格动态使用 XamlReader.Load



常量字符串XAML = @
<风格的xmlns =HTTP://schemas.microsoft.com/winfx/2006/xaml/演讲'
的xmlns:X =的http://schemas.microsoft.com/winfx/2006/xaml'
=的TargetType按钮>
< setter属性='模板' >
<! - 省略清晰--->
< /二传手>
< Style.Triggers>
<触发属性=IsMouseOver 值='真'>
< setter属性='背景'值=米色/>
< /触发>
< /Style.Triggers>
< /样式和GT;
变种编码=新ASCIIEncoding();
VAR字节= encoding.GetBytes(XAML);
VAR风格=(风格)XamlReader.Load(新的MemoryStream(字节));
Button1.Style =风格;


I am trying to make a simple mouseover effect on a button, It does change the color when mouse is over but the color is immediately changed to the default button background... how can I override this behavior?

this is my code:

Style myBtnStyle = new Style();
Trigger bla = new Trigger() { Property = IsMouseOverProperty, Value = true };
bla.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Black));
myBtnStyle.Triggers.Add(bla);
button2.Style = myBtnStyle;
解决方案

According to this post, that fancy animation is built into and to remove it, you're going to need to override the ControlTemplate for your Button. Fortunately, that isn't too hard. I used this post as source material and came up with the following Style that gives you the idea.

<Style x:Key="MouseOverButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ControlTemplate.Resources>
                    <Style x:Key="ShadowStyle">
                        <Setter Property="Control.Foreground" Value="LightGray" />
                    </Style>
                </ControlTemplate.Resources>
                <Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}">
                    <Grid >
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="contentShadow" Style="{StaticResource ShadowStyle}">
                            <ContentPresenter.RenderTransform>
                                <TranslateTransform X="1.0" Y="1.0" />
                            </ContentPresenter.RenderTransform>
                        </ContentPresenter>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Beige" />
        </Trigger>
    </Style.Triggers>
</Style>

Update:If you're dead set on applying the Style in code and you don't want to use a ResourceDictionary (probably the better way to do it), you can load the Style dynamically using XamlReader.Load:

            const string xaml = @"
<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
       xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
       TargetType='Button'>
    <Setter Property='Template'>
        <!--- Omitted For Clarity --->
    </Setter>
    <Style.Triggers>
        <Trigger Property='IsMouseOver' Value='True'>
            <Setter Property='Background' Value='Beige' />
        </Trigger>
    </Style.Triggers>
</Style>";
            var encoding = new ASCIIEncoding();
            var bytes = encoding.GetBytes(xaml);
            var style = (Style)XamlReader.Load(new MemoryStream(bytes));
            Button1.Style = style;

这篇关于WPF样式按钮鼠标悬停问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:10