C# 如何在WPF中的触发器检查或取消检查事件之后启用/禁用复选框?
我为复选框控件编写了一个自定义模板,选中时显示绿色,未选中时显示红色。我想在触发选中/取消选中事件后使复选框控件自身禁用3秒钟,然后恢复启用状态。如何将该功能集成到自定义模板中C# 如何在WPF中的触发器检查或取消检查事件之后启用/禁用复选框?,c#,wpf,checkbox,C#,Wpf,Checkbox,我为复选框控件编写了一个自定义模板,选中时显示绿色,未选中时显示红色。我想在触发选中/取消选中事件后使复选框控件自身禁用3秒钟,然后恢复启用状态。如何将该功能集成到自定义模板中 <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CheckBox}"> <Grid x:Nam
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid1">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid x:Name="grid">
<Rectangle HorizontalAlignment="Left" Height="22" Stroke="White" VerticalAlignment="Top" Width="22">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
<GradientStop Color="#FF86CC6E" Offset="0.022"/>
<GradientStop Color="#FF47E211" Offset="0.81"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" FontFamily="Arial Black" FontSize="10.667" Text="{TemplateBinding Content}" />
</Grid>
<ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
<Grid x:Name="grid1">
<Rectangle HorizontalAlignment="Left" Height="22" Stroke="White" VerticalAlignment="Top" Width="22">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
<GradientStop Color="#FFDE6666" Offset="0.022"/>
<GradientStop Color="#FFFB2611" Offset="0.81"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" FontFamily="Arial Black" FontSize="10.667" Text="{TemplateBinding Content}" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
至少有一种使用
触发器的方法,如下所示:
<ControlTemplate TargetType="{x:Type CheckBox}">
<ControlTemplate.Resources>
<Storyboard x:Key="ffIsEnabled" Storyboard.TargetProperty="IsEnabled">
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled">
<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource ffIsEnabled}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource ffIsEnabled}"/>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
<!-- ... -->
</ControlTemplate>
您想要xaml还是代码隐藏?无论是xaml还是代码隐藏都可以