我的應用程序是使用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;
}
}
}
視圖的XAML代碼丟失;只有最後三個令牌在問題中;你能用更多的XAML修改這個問題嗎? – 2011-01-19 06:08:36