积极答复者
如何使鼠标移入button时button不显示高亮变色效果?

问题
答案
-
这个根本原因是WPF button在默认的template里面做的效果,要想disable掉 最直接的就是改template或者重写
<Button Content="AAA" MouseEnter="Button_MouseEnter_1"> <Button.Template> <ControlTemplate TargetType="Button"> <Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Button.Template> </Button>
Sheldon _Xiao
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 zwtgg99 2013年1月29日 8:02
全部回复
-
这个根本原因是WPF button在默认的template里面做的效果,要想disable掉 最直接的就是改template或者重写
<Button Content="AAA" MouseEnter="Button_MouseEnter_1"> <Button.Template> <ControlTemplate TargetType="Button"> <Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Button.Template> </Button>
Sheldon _Xiao
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 zwtgg99 2013年1月29日 8:02
-
如果你想要保持Button原来的样子(例如XP里面显示XP的按钮,Windows Aero Glass中显示Aero Glass样式的按钮,Basic中显示Basic样式的按钮)并阻止变色,你需要绘制这个控件而不是使用样式:
在构造方法中调用这个方法:
private void CreateStaticButton() { Button button = new Button(); button.Height = 100; button.Width = 100; button.Content = "hello, world"; button.UpdateLayout(); rect.Fill = new VisualBrush(button) { Stretch = Stretch.None }; rect.UpdateLayout(); }
XAML的内容是:
<Window x:Class="WPFButtonDontChangeColourWhenPointerOver.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Rectangle Name="rect" /> </Grid> </Window>
-
如果你不需要保留系统默认的Button样式,你可以使用这个例子,它创建一个默认的Windows 应用商店应用的亮色按钮的样式(嘿,在WPF里面做些类似Store的东西也挺酷的,不是么?)已经删除了指针移到上方时的变色效果。
<Button Height="60" Width="100"> <Button.Style> <Style TargetType="ButtonBase"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ButtonBase"> <ControlTemplate.Resources> <Storyboard x:Key="pressedStoryboard"> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Null}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="Black" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(TextBlock.Foreground)"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="White" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="revokePressedStoryboard"> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="Black" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="White" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(TextBlock.Foreground)"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="Black" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </ControlTemplate.Resources> <Border x:Name="border" BorderBrush="Black" BorderThickness="2" Background="White"> <ContentPresenter x:Name="contentPresenter" VerticalAlignment="Center" TextBlock.TextAlignment="Center" TextBlock.Foreground="Black" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Trigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource pressedStoryboard}" /> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard Storyboard="{StaticResource revokePressedStoryboard}" /> </Trigger.ExitActions> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> <Button.Content>hello, world</Button.Content> </Button>
我的建议:在你的应用中保持风格统一,如果要舍弃原来系统默认的样式,请全部改用相同的样式。
另外,这个需求着实奇怪,为什么在指针移入按钮的时候不给用户视觉反馈呢?