如何向不具有Command Binding属性的Items控件添加命令绑定

如何向不具有Command Binding属性的Items控件添加命令绑定,binding,command,routed,Binding,Command,Routed,我使用MVVM模式,我有以下代码: <DataTemplate x:Key="ActivityEditTemplate"> <ItemsControl ItemsSource="{Binding Path=ActivityList}"> <ItemsControl.ItemTemplate> <DataTemplate>

我使用MVVM模式,我有以下代码:

<DataTemplate x:Key="ActivityEditTemplate">
        <ItemsControl ItemsSource="{Binding Path=ActivityList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>                       
                    <CheckBox Margin="2,0,2,0" Content="{Binding ActivityDescription}" IsChecked="{Binding IsSelected}" Command="vm:PatternViewModel.ActivityCommand" CommandParameter="{Binding ActivityTitle}">
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>

ItemsControl中的复选框需要允许多重选择。我有一个路由命令,可以很容易地在复选框上实现它,但是我只希望在items控件折叠时执行该命令。datatemplate是WPF datagrid中CellEditingTemplate的静态资源。我希望代码仍然采用MVVM模式。我认为鼠标离开事件调用命令时运气不佳。提前谢谢。

我不是WPF专家,我可能会误解你的问题

然而,我刚刚读到:

有另一种方法可以将视图中的控件与视图模型公开的命令对象相关联。只有某些控件可以通过command属性实际绑定到命令,尤其是从System.Windows.controls.Primitives.ButtonBase或System.Windows.controls.MenuItem派生的控件。如果要将命令附加到某个其他控件,或者要在按钮的单击事件以外的事件上调用命令,则可以使用Expression Blend交互触发器和System.Windows.Interactivity.InvokeCommandAction类


您可能想查看事件触发器

我不是WPF专家,我可能会误解你的问题

然而,我刚刚读到:

有另一种方法可以将视图中的控件与视图模型公开的命令对象相关联。只有某些控件可以通过command属性实际绑定到命令,尤其是从System.Windows.controls.Primitives.ButtonBase或System.Windows.controls.MenuItem派生的控件。如果要将命令附加到某个其他控件,或者要在按钮的单击事件以外的事件上调用命令,则可以使用Expression Blend交互触发器和System.Windows.Interactivity.InvokeCommandAction类


您可能想查看事件触发器

我再次添加了代码。谢谢,我又添加了代码。谢谢