选中/取消选中复选框时,复选框不会触发所选项目的列表框

时间:2011-01-19 05:49:30

标签: wpf mvvm listbox

我的应用程序是使用wpf MVVM模式开发的,其中我有一个列表框,显示一组要选中的操作,并选中复选框以选中/取消选中。每当选中/取消选中复选框时,我都需要获取所选项目。我将复选框的IsChecked属性绑定到我的模型中的属性,将listbox的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;                
        }
    }

}

2 个答案:

答案 0 :(得分:1)

因为您设置SelectionMode =“Multiple”,所以不能使用SelectedItem 您也无法绑定到SelectedItems,因为此属性是只读的。

并非所有内容都丢失,因为在您的代码中,您将IsSelected绑定到IsOperationSelected

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

现在,您可以使用IsOperationSelected处理所选项目,如以下示例中ViewModel中所示:

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

答案 1 :(得分:0)

  1. 您应该将IsChecked绑定到容器IsSelected的{​​{1}}属性
  2. 这样您就可以处理ListBoxItem的{​​{1}}事件,并对任何更改做出反应。 (使用SelectionChangedListBox找出所做的更改。)
  3. 一些代码示例:

    e.AddedItems

    背后:

    e.RemovedItems