AButton
不能被选中,也不能像你所说的那样被“选择”。它没有IsSelected
属性。如果您想要这种行为,那么您将需要使用 a ToggleButton
,或者甚至RadioButton
是Style
d 看起来像常规的 a Button
:
将此添加Style
到您的Resources
部分:
<Style TargetType="{x:Type RadioButton}">
<Setter Property="Foreground" Value="White" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border CornerRadius="3.5" Background="White" BorderBrush="Black" BorderThickness="1" Padding="1">
<Border Name="Border" CornerRadius="3" Background="Blue" Padding="10,0,10,2">
<ContentPresenter />
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="Border" Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
然后您可以使用RadioButton
看起来像Button
s 并满足您的要求的 s:
<StackPanel Orientation="Horizontal">
<RadioButton Content="One" />
<RadioButton Content="Two" />
<RadioButton Content="Three" />
<RadioButton Content="Four" />
</StackPanel>