リストボックスが起動されたときにチェックボックスを切り替えるリスト項目を作成しようとしています。バインディングが関係していない場合はこれを正常に実行できましたが、バインディングを追加すると、バインディングは更新されませんが、チェックボックスの ischecked は更新されます。ボックスをチェックし、IsSelectedName のバインドも更新して、ユーザーがチェックしたことを確認したいのです。また、チェックボックス IsSelectedName を右クリックすると更新されます。私のコードの最も単純なバージョンは次のとおりです。
<ListBox Grid.Column="1" Margin="20" ItemsSource="{Binding listitems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="CheckboxIsSelected" IsChecked="{Binding IsSelectedName, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter TargetName="CheckboxIsSelected" Property="IsChecked" Value="True"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
前もって感謝します
ビューモデルは次のとおりです。
public class ListTestVM
{
public List<listem> listitems { get; set; }
public ListTestVM()
{
listitems = new List<listem>()
{
new listem(){ Name = "Test", IsSelectedName = false },
new listem(){ Name = "Test1", IsSelectedName = true },
new listem(){ Name = "Test2", IsSelectedName = false },
new listem(){ Name = "Test3", IsSelectedName = true }
};
}
}
リスト項目クラスは次のとおりです。
public class listem :INotifyPropertyChanged
{
private bool _IsSelectedName;
public bool IsSelectedName
{
get { return _IsSelectedName; }
set
{
_IsSelectedName = value;
NotifyPropertyChanged("IsSelectedName");
}
}
public string Name { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}