【问题标题】:WPF MVVM Checkbox stop Command from firing on Data BindWPF MVVM 复选框停止命令在数据绑定上触发
【发布时间】:2014-03-19 14:11:08
【问题描述】:

我有 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();
    }

【问题讨论】:

  • 如何从列表中填充它?
  • 在上面的代码中,您可以看到对 IsChecked 属性所做的绑定。 PatientAoi 是 VM 中的一个对象。

标签: c# wpf data-binding mvvm


【解决方案1】:

我会删除 Interaction.Triggers 并改用 CheckBox.CommandandCommandParameter 属性。

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

【讨论】:

  • 马克...谢谢...以为我已经尝试过了。显然不是。再次感谢!
猜你喜欢
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
相关资源
最近更新 更多