【问题标题】:Trigger "Checked/Unchecked" not firing in three state checkbox触发“已选中/未选中”未在三态复选框中触发
【发布时间】:2016-09-03 18:21:48
【问题描述】:

我正在使用以下代码使用 MVVM 实现三态复选框:

<CheckBox IsChecked="{Binding
              Path=IsValueChecked, 
              Mode=TwoWay, 
              UpdateSourceTrigger=Explicit}"
          Margin="3,10,0,0"
          IsThreeState="True">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Checked">
      <i:InvokeCommandAction Command="{Binding Path=CheckCommand, 
                      RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
    </i:EventTrigger>

    <i:EventTrigger EventName="Unchecked">
      <i:InvokeCommandAction Command="{Binding Path=CheckCommand, 
                      RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</CheckBox>

在视图模型中,创建了一个类型为Nullable&lt;bool&gt; 的可通知属性“IsValueChecked”。问题是单击复选框时不会触发事件“已选中”和“未选中”。 我错过了什么,请提出建议。

【问题讨论】:

  • 很可能您的命令绑定无法解析。
  • 在绑定不起作用后查看输出窗口总是一个好主意。

标签: wpf checkbox mvvm threestate


【解决方案1】:

扩展上面 Funk 的评论,看起来您正在尝试绑定到 DataGrid 级别的 DataContext,但您的绑定实际上是要尝试绑定到 DataGrid 本身的属性。鉴于 DataGrid 没有 CheckCommand 属性,这将失败。

尝试将绑定路径更改为:

{Binding Path=DataContext.CheckCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2015-08-09
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多