您好,登錄后才能下訂單哦!
在C#中,你可以使用WinForms或WPF來實現復選框的全選和反選功能。這里我將為你提供WinForms和WPF兩種實現方法的示例代碼。
WinForms實現:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnToggleSelectAll_Click(object sender, EventArgs e)
{
ToggleSelectAll(checkBoxList1);
}
private void ToggleSelectAll(CheckBoxList checkBoxList)
{
bool isSelected = checkBoxList.CheckedItems.Count == checkBoxList.Items.Count;
foreach (var item in checkBoxList.Items)
{
item.Checked = !isSelected;
}
}
}
WPF實現:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<CheckBoxList x:Name="checkBoxList1" HorizontalAlignment="Left" Height="200" Margin="10,10,0,0" VerticalAlignment="Top" Width="760"/>
<Button Content="全選/反選" HorizontalAlignment="Left" Margin="10,210,0,0" VerticalAlignment="Top" Width="760" Click="btnToggleSelectAll_Click"/>
</Grid>
</Window>
private void btnToggleSelectAll_Click(object sender, RoutedEventArgs e)
{
ToggleSelectAll(checkBoxList1);
}
private void ToggleSelectAll(CheckBoxList checkBoxList)
{
bool isSelected = checkBoxList.CheckedItems.Count == checkBoxList.Items.Count;
foreach (var item in checkBoxList.Items)
{
item.IsChecked = !isSelected;
}
}
這樣,你就可以在WinForms和WPF應用程序中實現復選框的全選和反選功能了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。