C# IsChecked设置在第二次而不是第一次单击时(ToggleButton(XAML))
希望您能帮助我解决我的问题: 我有两个按钮和一个矩形,我想做的是: 1.将矩形移动到按钮的方向(RightButton将其向右移动,LeftButton将其向左移动)(这很好) 2.停用上一次按下以移动矩形的按钮(矩形为右,右按钮停用),此外,按钮颜色为红色。 3.激活之前停用的按钮(矩形位于右位置,左按钮处于活动状态),此外按钮颜色为绿色 按钮的模板C# IsChecked设置在第二次而不是第一次单击时(ToggleButton(XAML)),c#,wpf,xaml,C#,Wpf,Xaml,希望您能帮助我解决我的问题: 我有两个按钮和一个矩形,我想做的是: 1.将矩形移动到按钮的方向(RightButton将其向右移动,LeftButton将其向左移动)(这很好) 2.停用上一次按下以移动矩形的按钮(矩形为右,右按钮停用),此外,按钮颜色为红色。 3.激活之前停用的按钮(矩形位于右位置,左按钮处于活动状态),此外按钮颜色为绿色 按钮的模板 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xa
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type ToggleButton}" x:Key="DefaultToggleButtonTemplate" x:Name="DefaultToggleButtonTemplate">
<Style.Setters>
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="30"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}" >
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
<Rectangle x:Name="buttonFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stroke="{TemplateBinding Background}" RadiusX="5" RadiusY="5" StrokeThickness="1" Fill="Transparent"/>
<Rectangle x:Name="buttonBody" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stroke="Transparent" RadiusX="5" RadiusY="5" StrokeThickness="1" Fill="{TemplateBinding Background}"/>
<TextBlock x:Name="buttonText" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ResourceDictionary>
当前的行为是:
1.程序启动,按钮为灰色,矩形位于左侧位置,两个按钮均处于活动状态
2.按下一个按钮将导致沿该方向移动矩形,将按下的按钮涂成红色(另一个为绿色)并将其自身禁用(激活另一个)。
3.现在按下另一个按钮,重置按钮颜色(两个),将矩形移动到另一个位置(两个按钮仍处于活动状态)(这就是问题所在)
4.按下相同的按钮将导致2的预期行为
那么如何阻止3的重置行为呢
编辑:我能够找出问题的根源:在步骤3中,按下按钮不将IsChecked属性设置为true,在步骤4中,IsChecked属性按预期设置为true。问题是,为什么在步骤3中没有设置为true,或者我可以做些什么来实现它 代码中的基本问题是触发器的求值顺序以及它们之间的交互方式 当程序启动时,两个按钮都未选中,因此没有一个触发器适用。每个按钮都以其默认状态显示 当用户单击切换按钮时,发生的第一件事是切换按钮的状态。由于按钮以默认值
false
开始,其新值变为true
。在这一点上,您的触发器开始生效
例如,单击“右”按钮,将其自身的IsChecked
值切换为true
,并使“左”按钮的触发器将其IsChecked
值设置为false
。到目前为止,一切顺利。这就是你想要的:你点击的地方会有一个红色按钮,你想让用户点击下一步的地方会有一个绿色按钮。这是因为“右”按钮的IsChecked
触发器被激活(禁用并将其涂成红色),以及“左”按钮的RightButton.IsChecked
触发器(启用并将其涂成绿色)
但是,当点击“左”按钮时,事情就会出错。“左”b
<Window x:Class="StyleResourceDictionariesDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
</Window.Resources>
<StackPanel>
<Button Style="{StaticResource DefaultButtonTemplate}" Content="ok"/>
<Button Style="{StaticResource DarkDefaultButtonTemplate}" Content="cancel"/>
<Button Style="{StaticResource FreakShowButtonTemplate}" Content="FREak"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.Triggers>
<EventTrigger SourceName="RightButton" RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath Storyboard.TargetName="RectMatrixTransform" Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="False" Duration="0:0:0.100">
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry>
<PathFigure StartPoint="0,0">
<LineSegment Point="100,0"/>
</PathFigure>
</PathGeometry>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger SourceName="LeftButton" RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath Storyboard.TargetName="RectMatrixTransform" Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="False" Duration="0:0:0.100">
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry>
<PathFigure StartPoint="100,0">
<LineSegment Point="0,0"/>
</PathFigure>
</PathGeometry>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<ToggleButton Content="Right" Name="RightButton" Grid.Column="0" Grid.Row="0" >
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource DefaultToggleButtonTemplate}">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="Red"/>
</MultiTrigger.Setters>
</MultiTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=LeftButton, Path=IsChecked, NotifyOnSourceUpdated=True}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsChecked" Value="False"/>
<Setter Property="IsEnabled" Value="True"/>
<Setter Property="Background" Value="Green"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
<ToggleButton Content="Left" Name="LeftButton" Grid.Column="1" Grid.Row="0" Height="30" Width="100">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource DefaultToggleButtonTemplate}">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="Red"/>
</MultiTrigger.Setters>
</MultiTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=RightButton, Path=IsChecked, NotifyOnSourceUpdated=True}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsChecked" Value="False"/>
<Setter Property="IsEnabled" Value="True"/>
<Setter Property="Background" Value="Green"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
<Rectangle x:Name="RectangleToMove" Grid.Column="0" Grid.Row="1" Height="100" Width="20" Fill="Black" Stroke="Black">
<Rectangle.RenderTransform>
<MatrixTransform x:Name="RectMatrixTransform"/>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</StackPanel>
</Window>