在 WPF MVVM 中绑定到 CheckBox Click 事件

Bind to CheckBox Click event with in WPF MVVM

我正在使用 Caliburn.Micro 编写 WPF 应用程序。我想根据带复选框的列表框中选中的项目更新名单。

XAML:

<ListBox ItemsSource="{Binding Names}">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox Content="{Binding Value}" IsChecked="{Binding IsChecked}" />
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

IsChecked 绑定有效,因此我可以 运行 一种验证哪些元素已被检查并更新名称列表的方法。我的第一种方法是将命令绑定到复选框单击事件或 Checked/Unchecked,但我无法让它工作...

<CheckBox Content="{Binding Value}" IsChecked="{Binding IsChecked}" Click="{Binding UpdateNameList}" />

在我的 ViewModel 中:

public void UpdateNameList()
{
    // update list...
}

我在 运行 时间收到此错误:

error Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.

如何使用 Caliburn.Micro 绑定到复选框单击事件?还是我应该换一种方式来做?

谢谢

使用 Action.TargetWithoutContext 附加 属性 将 Action.Target 绑定到父视图模型,并使用 Message.Attach 连接该方法。这应该有效:

<CheckBox Content="{Binding Value}" IsChecked="{Binding IsChecked}"
        cal:Action.TargetWithoutContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}"
        cal:Message.Attach="UpdateNameList()" />