简体   繁体   English

在C#中单击selectAll时如何检查所有复选框

[英]How to check all the checkboxes when selectAll is clicked in c#

How to check all the checkboxes in listbox when Select_All button is clicked in c#. 在c#中单击Select_All按钮时,如何检查列表框中的所有复选框。

Here is my XAML : 这是我的XAML

<StackPanel Width="400" Orientation="Horizontal" Grid.Row="0">
    <AppBarButton x:Name="Btn_SelectAll" Margin="20,0,0,0" Label="Select All" Icon="SelectAll" Click="Btn_SelectAll_Click" FontWeight="Bold" />
    <AppBarButton x:Name="Btn_Delete" Margin="150,0,0,0" Label="Delete All" Icon="Delete" Click="DeleteAll_Click" FontWeight="Bold" />
</StackPanel>
<ListBox  x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Name="Option1CheckBox"  VerticalAlignment="Center" Margin="5,0,0,0" />
            <TextBlock x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="22" Foreground="White"/>
            <TextBlock x:Name="Age"   TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" FontSize="22" Text="{Binding Age}" />
        </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The data in listbox is populated from database, so when a user clicks on SelectAll all the checkboxes should get checked. 列表框中的数据是从数据库填充的,因此,当用户单击SelectAll时,应选中所有复选框。

Create a public/protected Field in your CodeBehind like 在您的CodeBehind中创建一个公共/受保护的字段,例如

protected bool SelectAll = false;

In Btn_SelectAll_Click eventhandler, set SelectAll = true; Btn_SelectAll_Click事件处理程序中,设置SelectAll = true;

Bind Option1CheckBox IsChecked property with SelectAll field like below: Option1CheckBox IsChecked属性与SelectAll字段绑定,如下所示:

<CheckBox Name="Option1CheckBox"  VerticalAlignment="Center" Margin="5,0,0,0" IsChecked="{Binding SelectAll}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM