2017-08-01 72 views
1

如何在c#中单击Select_All按钮时检查列表框中的所有复选框。如何在c中单击selectAll时检查所有复选框

这里是我的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> 

在ListBox中的数据从数据库填充,所以当用户点击全选所有的复选框都应该得到遏制。

+0

假设你是遵循MVVM模式(你说你正在使用绑定):创建一个命令并设置所有布尔值'= TRUE'。 –

回答

0

在你的代码隐藏像

protected bool SelectAll = false; 

创建一个公共/保护领域Btn_SelectAll_Click事件处理程序,设置SelectAll = true;

绑定Option1CheckBoxIsChecked财产与全选场象下面这样:

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

这没有工作:(。数据没有被绑定我猜 – Rachel

+0

SelectAll应该是一个get/set字段为: protected bool SelectAll {get; set} = false; – Wayne

相关问题