ToggleButton 是 WPF 中的一个按钮控件,它可以在选中和未选中两种状态之间切换,类似于一个开关。它可以用于表示二元状态(如开/关、启用/禁用)的切换。下面是 ToggleButton 控件的详细使用教程。
1. 基本用法
在 XAML 中使用 ToggleButton 非常简单,可以通过以下代码定义一个 ToggleButton:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ToggleButton Example" Height="200" Width="400">
<Grid>
<ToggleButton x:Name="myToggleButton"
Content="Toggle Me"
Width="100"
Height="30"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Grid>
</Window>
Content: 设置按钮上显示的文本或内容。IsChecked: 这个布尔属性表示ToggleButton是否处于选中状态(True为选中,False为未选中,Null为不确定状态)。Checked:ToggleButton切换到选中状态时触发的事件。Unchecked:ToggleButton切换到未选中状态时触发的事件。
2. 事件处理
你可以通过为 Checked 和 Unchecked 事件编写处理程序,来响应 ToggleButton 状态的变化:
<ToggleButton x:Name="myToggleButton"
Content=


1584

被折叠的 条评论
为什么被折叠?



