如何在触发MouseDown事件时禁用边框控件。我有以下XAML
,但Property Setter
在<{1}}中允许 ,我只能在XAML中执行此操作:
EventTrigger
答案 0 :(得分:1)
试试这个:
<Border x:Name="border">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0"
Storyboard.TargetProperty="(Border.IsEnabled)">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>False</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
我们使用Storyboard
来解决无法直接设置Setter
并让动画瞬间立即生效的问题。
<强>更新强>
下载链接:Here
另一次更新(在Border
上显示已停用的其他状态)
<Border x:Name="border"
BorderThickness="5">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Background"
Value="BurlyWood" />
<Setter Property="BorderBrush"
Value="Blue" />
<Style.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0"
Storyboard.TargetProperty="(Border.IsEnabled)">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>False</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Background"
Value="Gray" />
<Setter Property="BorderBrush"
Value="DarkGray" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
<Button Margin="100"
Content="Some Button" />
</Border>