0

リストボックスが起動されたときにチェックボックスを切り替えるリスト項目を作成しようとしています。バインディングが関係していない場合はこれを正常に実行できましたが、バインディングを追加すると、バインディングは更新されませんが、チェックボックスの 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));
        }
    }
}
4

2 に答える 2

1

listItem プロパティには、次のように ObservableCollection タイプを使用します。

public ObservableCollection<listem> listitems { get; set; }

そして、ListBox DataTemplate で、次のようにバインディング モードを TwoWayeMode に設定します。

<CheckBox Name="CheckboxIsSelected" IsChecked="{Binding IsSelectedName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />

トリガーを削除する

于 2013-10-15T16:28:43.513 に答える
0

基本クラスで INotifyPropertyChanged インターフェイスを使用していますか? PropertyChanged のトリガーは動作しますか? それがあなたの問題の鍵になると思います。

于 2013-10-15T14:13:18.240 に答える