简体   繁体   中英

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#.

Here is my 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.

Create a public/protected Field in your CodeBehind like

protected bool SelectAll = false;

In Btn_SelectAll_Click eventhandler, set SelectAll = true;

Bind Option1CheckBox IsChecked property with SelectAll field like below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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