我有一个带有一些按钮的菜单栏导航到不同部分的WPF应用程序。在WPF中处理按钮状态
所有按钮都具有VisualState属性的样式,因此它们都具有Normal,OnMouseOver和Pressed状态不同的样式。所有按钮,一旦按下,在ModelView上执行一个命令。
我想要的是每次按下按钮时,首先它会保持按下状态,直到按下另一个按钮,其次,可以在ModelView侧执行的Command事件中更改按钮状态。
验证码:
<Style x:Key="MainButtonStyle" TargetType="Button">
<Setter Property="Width" Value="120"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="White" x:Name="ButtonGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0:0:0.3" Storyboard.TargetName="ButtonGrid" Storyboard.TargetProperty="Background.Color" To="Cyan"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="ButtonGrid" Storyboard.TargetProperty="Background.Color" To="DarkSalmon"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
使用样式的按钮:
<ItemsControl Margin="10" Grid.Column="1" Grid.RowSpan="2" ItemsSource="{Binding HeaderButtons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource MainButtonStyle}" Content="{Binding content}"
Command="{Binding DataContext.ChangePageContainerCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding containerType}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
,我认为该模型视图是不相关的,因为它在按钮上的VisualState,因为我不知道什么如何将它传递给Button调用者的引用(作为参数或其他?)。
我在其他帖子中看到人们推荐使用ToggleButton代替Button并将IsChecked设置为true,但我不知道应该在哪里设置该属性,我的意思是,我应该将它设置为XAML还是代码?如何?