2017-08-01 105 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

相關問題