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