2011-01-19 23 views
1

我的應用程序是使用wpf MVVM模式開發的,其中我有一個列表框,其中顯示了使用複選框選擇要檢查/取消選中的一組操作。每當複選框被選中/取消選中時,我需要獲取選定的項目。我將複選框的IsChecked屬性綁定到我的模型中的屬性,並將列表框的selecteditem屬性綁定到我的viewmodel中的屬性。每當我選中/取消選中列表中的第一個項目時,所選擇的項目事件就會觸發,但是當我檢查/取消選中列表中第一個選定項目以外的任何其他項目時,同樣的情況不會被觸發。每當用戶對列表框項目進行任何更改時,我都需要捕獲這些更改。 這是我的觀點:當複選框被選中/取消選中時,帶有複選框的列表框不會觸發選定的項目

<ListBox Height="280" Width="Auto" ItemsSource="{Binding OperationsInfoCol}" SelectionMode="Multiple" 
         SelectedItem="{Binding Path=SelectedOperationItem,UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding CanEnableListBox}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Content="{Binding OperationName}" 
               IsChecked="{Binding Path=IsOperationSelected,Mode=TwoWay}" IsEnabled="{Binding Path=CanEnableOperation,Mode=TwoWay}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="IsSelected" Value="{Binding IsOperationSelected,Mode=TwoWay}"/> 
      <Setter Property="IsEnabled" Value="{Binding CanEnableOperation,Mode=TwoWay}"/> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Background" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 

     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

視圖模型:

public OperationsInfo SelectedOperationItem 
    { 
     get 
     { 
      return m_oOperationSelected; 
     } 
     set 
     { 
      if (value != null) 
      { 
       m_oOperationSelected = value; 
       OnPropertyChanged("SelectedOperationItem"); 
       if (null != m_oOperationSelected) 
       { 
        ObservableCollection<OperationsInfo> oCol = new ObservableCollection<OperationsInfo>(); 
        //if (m_oOperationSelected.CanEnableOperation) 
        { 
         foreach (OperationsInfo itm in OperationsInfoCol) 
         { 
          if (itm.OperationId == m_oOperationSelected.OperationId && m_oOperationSelected.CanEnableOperation) 
          { 
           itm.IsOperationSelected = !m_oOperationSelected.IsOperationSelected; 
          } 
          oCol.Add(itm); 
         } 

         OperationsInfoCol.Clear(); 
         OperationsInfoCol = oCol; 
        } 
       } 
      } 
     } 
    } 

型號:

public class OperationsInfo { 

    private string m_strOperationName; 
    private int m_nOperationId; 
    private bool m_bIsOperationSelected; 
    private bool m_bCanEnable; 
    private LicenseManagerViewModel m_VMLicenseManager; 


public bool IsOperationSelected 
    { 
     get 
     { 
      return m_bIsOperationSelected; 
     } 
     set 
     {     
      m_bIsOperationSelected = value;     
      LicenseManagerVM.OperationInfoChecked = value;     
     } 
    } 

} 
+0

視圖的XAML代碼丟失;只有最後三個令牌在問題中;你能用更多的XAML修改這個問題嗎? – 2011-01-19 06:08:36

回答

0
  1. 你或許應該綁定IsChecked到容器的IsSelected財產ListBoxItem
  2. 通過這種方式,您可以處理ListBoxSelectionChanged事件,並對任何更改作出反應。 (使用e.AddedItemse.RemovedItems找出所做的修改。)

一些代碼示例:

 <ListBox ItemsSource="{Binding Data}" SelectionChanged="ListBox_SelectionChanged" SelectionMode="Extended"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <CheckBox Content="{Binding Name}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"/>    
      </DataTemplate>   
     </ListBox.ItemTemplate> 
    </ListBox> 

背後:

private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) 
    { 
     ListBox lb = sender as ListBox; 
     if (e.AddedItems.Count > 0) 
     { 
      foreach (Employee emp in e.AddedItems.Cast<Employee>()) MessageBox.Show("Added: " + emp.Name); 
     } 
     if (e.RemovedItems.Count > 0) 
     { 
      foreach (Employee emp in e.RemovedItems.Cast<Employee>()) MessageBox.Show("Removed: " + emp.Name); 
     } 
    } 
+1

我們正在關注MVVM模式,我還需要使用MVVM模式來處理selectionchanged。你建議的代碼behinf跟着沒有MVVM我猜..我是否正確?請告訴我一些選擇。 – sri 2011-01-19 09:07:32

1

因爲你設置的SelectionMode = 「多」,你不能使用SelectedItem。
您也無法綁定到SelectedItems,因爲此屬性是隻讀的。

並非所有的在你的代碼,因爲失去了你綁定IsSelected到IsOperationSelected

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="IsSelected" 
       Value="{Binding IsOperationSelected,Mode=TwoWay}"/> 
    </Style> 
</ListBox.ItemContainerStyle> 

所以,現在你可以處理使用IsOperationSelected選擇的項目,如在你的視圖模型下面的例子表明:

foreach (var operationsInfo in OperationsInfoCol) 
{ 
    if (operationsInfo.IsOperationSelected) 
    { 
    // do something... 
    } 
} 
相關問題