【问题标题】:How to add Command Binding to an Items control that does not have the command binding property如何将命令绑定添加到没有命令绑定属性的项控件
【发布时间】:2010-12-17 01:54:58
【问题描述】:

我使用 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 中的复选框需要允许多选。我有一个路由命令,可以在复选框上轻松实现它,但是我只希望在项目控件折叠时执行该命令。数据模板是 WPF 数据网格中 CellEditingTemplate 的静态资源。我希望代码仍然采用 MVVM 模式。我认为鼠标离开事件调用命令没有运气。提前致谢。

【问题讨论】:

  • 我又添加了代码。谢谢

标签: binding command routed


【解决方案1】:

我不是 WPF 专家,我可能会以错误的方式阅读您的问题。

但是,我刚刚读到这个:

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

Source: blog.magnusmontin.net

您可能想查看事件触发器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2016-08-12
    • 1970-01-01
    • 2011-10-04
    • 2014-05-10
    • 2017-06-04
    • 2010-10-30
    相关资源
    最近更新 更多