3 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
要?jiǎng)h除默認(rèn)MouseOver
行為,Button
您需要修改ControlTemplate
。將您的Style
定義更改為以下內(nèi)容應(yīng)該可以解決問題:
<Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="Green"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Red"/> </Trigger> </Style.Triggers></Style>
編輯:它已經(jīng)晚了幾年,但你實(shí)際上可以在那里的邊框內(nèi)設(shè)置邊框畫筆。Idk如果有人指出但它似乎不是......

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
到目前為止,所有答案都涉及用其他東西完全替換默認(rèn)按鈕行為。但是,恕我直言,通過編輯XAML元素的現(xiàn)有默認(rèn)模板,了解可以僅更改您關(guān)注的部分是有用且重要的。
在處理上的WPF按鈕懸停效果的情況下,在一個(gè)WPF在外觀上的變化Button
元件通過引起Trigger
在用于默認(rèn)的樣式Button
,這是基于所述IsMouseOver
屬性,并設(shè)置Background
和BorderBrush
頂層的性質(zhì)Border
元件在控件模板中。該Button
元素的背景是下面Border
元素的背景,因此更改Button.Background
屬性不會(huì)阻止看到懸停效果。
通過一些努力,您可以使用自己的setter覆蓋此行為,但是因?yàn)槟枰绊懙脑卦谀0逯卸荒茉谀约旱腦AML中直接訪問,所以這種方法很難并且IMHO過于復(fù)雜。
另一種選擇是利用圖形作為Content
為Button
,而不是Background
。如果您需要在圖形上添加其他內(nèi)容,則可以將它們與Grid
作為內(nèi)容中的頂級(jí)對(duì)象的組合使用。
但是,如果您只想完全禁用懸停效果(而不是僅隱藏它),則可以使用Visual Studio XAML設(shè)計(jì)器:
編輯XAML時(shí),選擇“設(shè)計(jì)”選項(xiàng)卡。
在“設(shè)計(jì)”選項(xiàng)卡中,找到要禁用其效果的按鈕。
右鍵單擊該按鈕,然后選擇“編輯模板/編輯副本...”。在提示中選擇您希望放置新模板資源的位置。這似乎什么都不做,但實(shí)際上Designer會(huì)在你告訴它的地方添加新資源,并改變你的button元素以引用使用這些資源作為按鈕模板的樣式。
現(xiàn)在,您可以編輯該樣式。最簡單的方法是刪除或注釋掉(例如Ctrl+ E,C)
<Trigger Property="IsMouseOver" Value="true">...</Trigger>
元素。當(dāng)然,您可以在此時(shí)對(duì)所需的模板進(jìn)行任何更改。
完成后,按鈕樣式將如下所示:
<p:Style x:Key="FocusVisual"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> </ControlTemplate> </Setter.Value> </Setter></p:Style><SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/><SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/><SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/><SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/><SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/><SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/><SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/><SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/><SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/><p:Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsDefaulted" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> <!--<Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/> </Trigger>--> <Trigger Property="IsPressed" Value="true"> <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/> <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter></p:Style>
(注意:您可以省略p:
實(shí)際代碼中的XML命名空間限定...我在此提供它們只是因?yàn)镾tack Overflow XML代碼格式化程序被<Style/>
沒有XML命名空間的完全限定名稱的元素所混淆。)
如果要將相同的樣式應(yīng)用于其他按鈕,只需右鍵單擊它們并選擇“編輯模板/應(yīng)用資源”,然后選擇剛為第一個(gè)按鈕添加的樣式。您甚至可以使用常規(guī)技術(shù)將默認(rèn)樣式應(yīng)用于XAML中的元素,從而使該樣式成為所有按鈕的默認(rèn)樣式。
添加回答
舉報(bào)