单击 WPF 后更改按钮的效果 [英] Change the Effect of Button once it is clicked WPF
本文介绍了单击 WPF 后更改按钮的效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按下 Button
时应用以下更改.
I want to apply the following changes when the Button
is pressed.
<Button>
<Button.Effect>
<DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
</Button.Effect>
</Button>
推荐答案
如何处理 Button
的 Click
事件?
What about handling the Click
event for the Button
?
<Button Content="Click me!" Click="Button_Click_1" Margin="10" />
<小时>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
button.Effect = new System.Windows.Media.Effects.DropShadowEffect()
{
BlurRadius = 10,
ShadowDepth = 5
};
}
或者,如果您只想在按下按钮时显示阴影,请使用绑定到 IsPressed
属性的 Style
:
Or use a Style
that binds to the IsPressed
property if you want to display the shadow only while the Button is being pressed:
<Button Content="Button">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
或者使用 ToggleButton
并根据其 IsChecked
属性设置 Effect
属性:
Or use a ToggleButton
and set the Effect
property based on its IsChecked
property:
<ToggleButton>
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
这篇关于单击 WPF 后更改按钮的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文