WPF MVVM复选框阻止命令在数据绑定上触发

本文关键字:数据绑定 命令 MVVM 复选框 WPF | 更新日期: 2023-09-27 17:50:54

我有WPF MVVM应用程序,我目前正在尝试使用一个复选框,该复选框绑定到它绑定到的列表中的列。我有一个EventTriggers设置和绑定到VM的命令。一切都很棒....除了我不希望事件在从列表中填充时触发,只有当用户选中或取消选中复选框时才触发。看到代码:

    <StackPanel Orientation="Vertical" Background="Transparent" DockPanel.Dock="Right" Margin="2,0" VerticalAlignment="Center">
        <Label Content="Active" />
        <CheckBox x:Name="CbArchiveAoi" VerticalAlignment="Center" HorizontalAlignment="Center" FlowDirection="RightToLeft" IsChecked="{Binding Path=PatientAoi.AoiIsActive}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <i:InvokeCommandAction Command="{Binding ArchiveAoiCommand, Mode=OneWay}" 
                                       CommandParameter="{Binding ElementName=CbArchiveAoi}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="Unchecked">
                     <i:InvokeCommandAction Command="{Binding ArchiveAoiCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=CbArchiveAoi}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </CheckBox>
    </StackPanel>
    public ICommand ArchiveAoiCommand
    {
      get { return new RelayCommand<object>(ArchiveAoiExecute, AlwaysTrueCanExecute); }
    }
    private void ArchiveAoiExecute(object obj)
    {
        string dddd = obj.ToString();
    }

WPF MVVM复选框阻止命令在数据绑定上触发

我将删除交互。触发并使用CheckBox.CommandCommandParameter属性。

<CheckBox x:Name="CbArchiveAoi"
          VerticalAlignment="Center"
          <-- snip -->
          Command="{Binding ArchiveAoiCommand, Mode=OneWay}"
          CommandParameter="{Binding ElementName=CbArchiveAoi}" />