2016-05-13 80 views
0

我在wpf mvvm模式中使用了其SelectedItem绑定完成ViewModel的ListView。我面临的问题是,只要我检查复选框,SelectedItem绑定不能立即工作。只有当我再次点击复选框及其相应内容以外的地方时才能使用。列表视图在wpf中选择的项绑定mvvm

我的ListView是这样的:

<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与我绑定的列表视图中选择的ITM是这样的:

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

这是Rachel Lim的一个很好的例子,试图实现... 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> 

而对于这项工作,你需要把SelectionMode到单

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

而且这样做在你的视图模型

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