2016-05-13 30 views
0

私は、ViewModelに対してSelectedItemバインディングが行われているwpf mvvmパターンでListViewを使用しています。私が直面している問題は、チェックボックスをチェックするとすぐに、SelectedItemバインディングがすぐには機能しないことです。それは、チェックボックスの外側のどこかを再度クリックしたときにのみ機能します。選択したアイテムを表示wpf mvvmでバインド

私のListViewは、このようなものです:私は、リストビューの選択ITMを結合していたと

<Grid> 
      <Grid.Resources> 
       <DataTemplate x:Key="checkboxHeaderTemplate"> 
        <CheckBox IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=UserControl },Mode=TwoWay}"> 
        </CheckBox> 
       </DataTemplate> 

       <DataTemplate x:Key="CheckBoxCell"> 
        <!--<CheckBox Checked="CheckBox_Checked" />--> 
        <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > 
        </CheckBox> 
       </DataTemplate> 

       <DataTemplate x:Key="TextCell"> 
        <TextBlock Text="Usecasename"> 
        </TextBlock> 
       </DataTemplate> 

       <DataTemplate x:Key="ButtonCell"> 
        <Button Content="{Binding Path=UsecaseName, Mode=TwoWay}" > 
        </Button> 
       </DataTemplate> 
      </Grid.Resources> 

      <ListView SelectedItem="{Binding SelectedSection}" ItemsSource="{Binding Path=UsecaseListItems}" > 
       <ListView.View> 
        <GridView> 
         <GridView.Columns> 

          <GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}" 
             CellTemplate="{StaticResource CheckBoxCell}" Width="auto"> 
          </GridViewColumn> 

          <GridViewColumn HeaderTemplate="{StaticResource TextCell}" 
             CellTemplate="{StaticResource ButtonCell}" Width="auto"> 
          </GridViewColumn> 
         </GridView.Columns> 
        </GridView> 
       </ListView.View> 
      </ListView> 
     </Grid> 

HomeViewModelは、このようなものです:

private UseCase _selectedSection; 
     public UseCase SelectedSection 
     { 
      get { return _selectedSection; } 
      set 
      { 
       _selectedSection = value; 
       if (this.SelectedSection.UsecaseName == ("CCS01") && (this.SelectedSection.IsSelected == true)) 
       { 
        this.ContentWindow = new CCS01(); 
       } 
       else if (this.SelectedSection.UsecaseName == ("CCS02") && (this.SelectedSection.IsSelected == true)) 
       { 
        this.ContentWindow = new CCS02(); 
       } 
       else if (this.SelectedSection.UsecaseName == ("ECS52") && (this.SelectedSection.IsSelected == true)) 
       { 
        this.ContentWindow = new ECS52(); 
       } 
       else 
        this.ContentWindow = new Default(); 
       OnPropertyChanged("SelectedSection"); 
      } 
     } 

とユースケースのクラスがこれです:

public class UseCase: BaseNotifyPropertyChanged 
    { 
     public string UsecaseName { get; set; } 



     private bool _IsSelected; 
     public bool IsSelected 
     { 
      get { return _IsSelected; } 
      set 
      { 
       _IsSelected = value; 
       OnPropertyChanged("IsSelected"); 

      } 
     } 




    } 

訂正を提案してください私はチェックボックスをチェックします。

+0

これはレイチェム・リムの良い例です。達成しようとしています... https://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/ – Monty

答えて

0

あなたはこのような何かにCheckBoxCellの結合チェックボックスに変更する必要があります

<DataTemplate x:Key="CheckBoxCell"> 
       <!--<CheckBox Checked="CheckBox_Checked" />--> 
       <CheckBox IsChecked="{Binding Path=IsSelected, 
          RelativeSource={RelativeSource FindAncestor, 
        AncestorType={x:Type ListViewItem}}, 
          Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > 
       </CheckBox> 
      </DataTemplate> 

そして、あなたはシングル

<ListView SelectedItem="{Binding SelectedSection}" 
ItemsSource="{Binding Path=UsecaseListItems}" SelectionMode="Single"> 

SelectionModeを入れて、あなたのviewmodel

でこれを行う必要があり、この作業のために
private UseCase _selectedSection; 
     public UseCase SelectedSection 
     { 
      get { return _selectedSection; } 
      set 
      { 
       DeSelectAll(); 
       _selectedSection = value; 
       _selectedSection.IsSelected = true; 
       OnPropertyChanged("SelectedSection"); 
      } 
     } 

     private void DeSelectAll() 
     { 
      foreach (var item in UsecaseListItems) 
      { 
       item.IsSelected = false; 
      } 
     } 
+0

クリックする代わりに別のアイテムを選択すると、SelectedItem.IsSelectedプロパティの値はどうなりますかCheckBox? – Davy

関連する問題