2011-01-19 10 views
1

私のアプリケーションは、チェックボックスをオンまたはオフにするチェックボックスで選択する操作のリストボックスを持ったwpf MVVMパターンを使用して開発されます。チェックボックスをオン/オフにするたびに、選択した項目を取得する必要があります。私はチェックボックスのIsCheckedプロパティを自分のモデルのプロパティにバインドしています。リストボックスのselecteditemプロパティをビューモデルのプロパティにバインドしています。リスト内の最初のアイテムをチェック/チェック解除すると、選択されたアイテムイベントがトリガされますが、リスト内の最初に選択されたアイテム以外のアイテムをチェック/チェック解除するとトリガされません。ユーザーがリストボックス項目を変更するたびに変更をキャプチャする必要があります。 は、ここに私の見解である:チェックボックスがチェックボックスのオン/オフになっていると、チェックボックスが選択されていないリストボックス

<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> 

のViewModel:

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; 
        } 
       } 
      } 
     } 
    } 

モデル:おそらくコンテナのIsSelectedプロパティにIsCheckedをバインドする必要があり

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コードがありません。最後の3つのトークンだけが問題になります。より多くのXAMLで質問を修正できますか? –

答えて

0
  1. ListBoxItem
  2. そのようにすれば、のSelectionChangedイベントを処理し、変更に対応することができます。 (作られた場所に変更何かを見つけるために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パターンを使用しても選択変更を処理する必要があります。あなたが提案したコードbehinfはMVVMにはないと思います。私は正しいのですか?私にはいくつかの選択肢があります。 – sri

+0

コードはかなりデザインパターンに似ていませんが、SelectionChangedを処理するときに行うことはあなた次第であり、MVVMに準拠しているかどうかによって異なります。 –

1

をあなたは= "複数" のselectionModeを設定しているので、あなたはSelectedItemは使用できません。
このプロパティは読み取り専用なので、SelectedItemsにバインドすることもできません。あなたのViewModelで、次の例に示すように、すべてがあなたのコードであるために失われるわけではありません

あなたがIsOperationSelectedを使用して、選択した項目を処理できるようになりましたIsOperationSelected

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

をIsSelectedをバインド:

foreach (var operationsInfo in OperationsInfoCol) 
{ 
    if (operationsInfo.IsOperationSelected) 
    { 
    // do something... 
    } 
}