2017-07-18 19 views
0

すべてのチェックボックスを「すべてのもの」でチェックボックスの名前を選択して選択します。 チェックボックスは、リストボックスにすべてのチェックボックスを選択するWPF

<ListBox SelectionMode="Multiple" 
     BorderThickness="0" 
     ItemsSource="{Binding QuestionThreeSelection}" 
     SelectedItem="{Binding QuestionThreeSelection}" 
     Name="listBoxList" 
     SelectionChanged="listBoxList_SelectionChanged"> 
    <ListBox.InputBindings> 
     <KeyBinding Command="ApplicationCommands.SelectAll" 
        Modifiers="Ctrl" 
        Key="A" /> 
    </ListBox.InputBindings> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Checked="CheckBox_Checked_1" 
         Content="{Binding SourceName}" 
         IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

バックコード

private void CheckBox_Checked_1(object sender, RoutedEventArgs e) 
{   
    var oo = listBoxList; 
    CheckBox cb = (CheckBox)sender; 
    //var w=e; 

    IEnumerable<AddSource> listallityem = ((IEnumerable<AddSource>)listBoxList.Items.SourceCollection).Where(r => r.IsSelected == false); 
    //IEnumerable<AddSource> listallityem1 = ((IEnumerable<AddSource>)listBoxList.Items.SourceCollection); 

    AddSource vv = cb.DataContext as AddSource; 
    if ((bool) cb.IsChecked) 
    { 

    } 

    if (vv.SourceName== "All of the above") 
    { 
     r = listBoxList.ItemsSource; 

     foreach (AddSource item in wer) 
     { 
      item.IsSelected = true; // false in case of unselect 
     } 
    } 
} 

ている誰かが方法を提案することはできますか?あなたのAddSourceクラスがINotifyPropertyChangedを実装していることを確認してくださいとのセッターでPropertyChangedイベントを発生させ

private void CheckBox_Checked(object sender, RoutedEventArgs e) 
{ 
    SelectAll(true); 
} 

private void CheckBox_Unchecked(object sender, RoutedEventArgs e) 
{ 
    SelectAll(false); 
} 

private void SelectAll(bool select) 
{ 
    var all = listBoxList.ItemsSource as IEnumerable<AddSource>; 
    if (all != null) 
    { 
     foreach (var source in all) 
      source.IsSelected = select; 
    } 
} 

+0

Bindingを持っているのでViewModelですべてを処理できます。 – Rekshino

答えて

1

あなたは、このようなあなたの「上記のすべて」CheckBox何かをCheckedUncheckedイベントを扱うことができますIsSelectedプロパティ。

+0

はい正確な理由は、私はimplemet INotifyPropertyChanged.nowそれは動作しませんでした..ありがとう – MSKP

関連する問題