1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
依賴屬性概述:
綁定被視為本地值,這意味著如果您設(shè)置另一個(gè)本地值,您將消除綁定。有關(guān)詳細(xì)信息,請(qǐng)參閱依賴屬性值優(yōu)先級(jí)。
如果您對(duì)該Click
事件感興趣,那么您應(yīng)該使用EventTrigger
(或)使用 XAMLVisualStateManager
來(lái)操作狀態(tài):Button
<Style x:Key="MainMenuStyle"
? ? ? ?TargetType="{x:Type Button}">
? <Setter Property="Background"
? ? ? ? ? Value="#FF303136" />
? <Setter Property="Foreground"
? ? ? ? ? Value="#FF6F7074" />
? <Setter Property="Template">
? ? <Setter.Value>
? ? ? <ControlTemplate TargetType="{x:Type Button}">
? ? ? ? <Border x:Name="ButtonBorder" Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? Padding="30, 0, 0, 0">
? ? ? ? ? <ContentPresenter HorizontalAlignment="Left"
? ? ? ? ? ? ? ? ? ? ? ? ? ? VerticalAlignment="Center" />
? ? ? ? </Border>
? ? ? ? <ControlTemplate.Triggers>
? ? ? ? ? <EventTrigger RoutedEvent="Click">
? ? ? ? ? ? <BeginStoryboard>
? ? ? ? ? ? ? <Storyboard>
? ? ? ? ? ? ? ? <ColorAnimation Storyboard.TargetName="ButtonBorder"?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Duration="0:0:0"?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? To="Coral" />
? ? ? ? ? ? ? ? <ColorAnimation Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Duration="0:0:0"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? To="White" />
? ? ? ? ? ? ? </Storyboard>
? ? ? ? ? ? </BeginStoryboard>
? ? ? ? ? </EventTrigger>
? ? ? ? </ControlTemplate.Triggers>
? ? ? </ControlTemplate>
? ? </Setter.Value>
? </Setter>
</Style>
但如果您只想提供按鈕單擊反饋,您應(yīng)該在該Button.IsPressed屬性上觸發(fā):
<Style x:Key="MainMenuStyle"
? ? ? ?TargetType="{x:Type Button}">
? <Setter Property="Background"
? ? ? ? ? Value="#FF303136" />
? <Setter Property="Foreground"
? ? ? ? ? Value="#FF6F7074" />
? <Setter Property="Template">
? ? <Setter.Value>
? ? ? <ControlTemplate TargetType="{x:Type Button}">
? ? ? ? <Border x:Name="ButtonBorder" Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? Padding="30, 0, 0, 0">
? ? ? ? ? <ContentPresenter HorizontalAlignment="Left"
? ? ? ? ? ? ? ? ? ? ? ? ? ? VerticalAlignment="Center" />
? ? ? ? </Border>
? ? ? ? <ControlTemplate.Triggers>? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? <Trigger Property="IsPressed" Value="True">
? ? ? ? ? ? <Setter Property="Background"
? ? ? ? ? ? ? ? ? ? Value="Coral" />
? ? ? ? ? ? <Setter Property="Foreground"
? ? ? ? ? ? ? ? ? ? Value="White" />
? ? ? ? ? </Trigger>
? ? ? ? </ControlTemplate.Triggers>
? ? ? </ControlTemplate>
? ? </Setter.Value>
? </Setter>
</Style>
編輯
背景色之所以被設(shè)置回來(lái),是因?yàn)橛|發(fā)器的原因IsMouseOver。您所描述的從Button.Click事件處理程序切換樣式的解決方案并不干凈,無(wú)法回答您的問(wèn)題。
您希望Button單擊(選擇)時(shí)更改顏色,同時(shí)應(yīng)用鼠標(biāo)懸停效果。
因此我建議替換Button為ToggleButton. ToggleButton提供兩種開箱即用的狀態(tài)(選中和未選中):
<Style x:Key="MainMenuStyle"
? ? ? ?TargetType="{x:Type ToggleButton}">
? <Setter Property="Background"
? ? ? ? ? Value="#FF303136" />
? <Setter Property="Foreground"
? ? ? ? ? Value="#FF6F7074" />
? <Setter Property="Template">
? ? <Setter.Value>
? ? ? <ControlTemplate TargetType="{x:Type ToggleButton}">
? ? ? ? <Border x:Name="ButtonBorder" Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? Padding="30, 0, 0, 0">
? ? ? ? ? <ContentPresenter HorizontalAlignment="Left"
? ? ? ? ? ? ? ? ? ? ? ? ? ? VerticalAlignment="Center" />
? ? ? ? </Border>
? ? ? ? <ControlTemplate.Triggers>
? ? ? ? ? <Trigger Property="IsChecked" Value="True">
? ? ? ? ? ? <Setter Property="Background"
? ? ? ? ? ? ? ? ? ? Value="Coral" />
? ? ? ? ? ? <Setter Property="Foreground"
? ? ? ? ? ? ? ? ? ? Value="White" />? ??
? ? ? ? ? </Trigger>
? ? ? ? ? <Trigger Property="IsMouseOver" Value="True">
? ? ? ? ? ? <Setter Property="Background"
? ? ? ? ? ? ? ? ? ? Value="#FF2A2A32" />
? ? ? ? ? ? <Setter Property="Foreground"
? ? ? ? ? ? ? ? ? ? Value="#FFBBBCB8" />? ??
? ? ? ? ? </Trigger>
? ? ? ? ? <MultiTrigger>
? ? ? ? ? ? <MultiTrigger.Conditions>
? ? ? ? ? ? ? <Condition Property="IsChecked" Value="True" />
? ? ? ? ? ? ? <Condition Property="IsMouseOver" Value="True" />
? ? ? ? ? ? </MultiTrigger.Conditions>
? ? ? ? ? ? <Setter Property="Background"
? ? ? ? ? ? ? ? ? ? Value="Coral" />
? ? ? ? ? ? <Setter Property="Foreground"
? ? ? ? ? ? ? ? ? ? Value="#FFBBBCB8" />
? ? ? ? ? </MultiTrigger>
? ? ? ? </ControlTemplate.Triggers>
? ? ? </ControlTemplate>
? ? </Setter.Value>
? </Setter>? ??
</Style>
- 1 回答
- 0 關(guān)注
- 83 瀏覽
添加回答
舉報(bào)