私のアプリケーションは、チェックボックスをオンまたはオフにするチェックボックスで選択する操作のリストボックスを持った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;
}
}
}
ビューのXAMLコードがありません。最後の3つのトークンだけが問題になります。より多くのXAMLで質問を修正できますか? –