我为复选框控件编写了一个自定义模板,它在选中时显示绿色,在未选中时显示红色。我希望复选框控件在触发检查/取消选中事件后使其自身禁用 3 秒,然后恢复自身启用。如何才能我将该功能集成到我的自定义模板中吗?
<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>
关于c# - 如何在 WPF 中触发检查或取消选中事件后启用/禁用复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965489/