首页 > 解决方案 > DataTemplate 中的 Command- 和 InputBindings

问题描述

我有一个 WPF UserControl,我试图用一种 MVVM 模式来适应我的程序。现在的问题是,UserControl 使用了我不能简单地应用于 DataTemplate 的 Command- 和 InputBindings。在没有代码隐藏的情况下,我必须有哪些选项来实现相同的功能?

这是目前的 XAML:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:MyProject.Settings"
                xmlns:elements="clr-namespace:MyProject.Settings.Elements"
                xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
                xmlns:dataPresenter="http://infragistics.com/DataPresenter"
                xmlns:igEditors="http://infragistics.com/Editors"
                xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<DataTemplate x:Key="PrioritySettingTemplate" DataType="{x:Type elements:PrioritySetting}">
    <ContentPresenter>
        <ContentPresenter.InputBindings>
            <KeyBinding Gesture="Delete" Key="Delete"
                        Command="{Binding TestCommand}" />
        </ContentPresenter.InputBindings>
        <ContentPresenter.ContentTemplate>
            <DataTemplate DataType="{x:Type elements:PrioritySetting}">
                    <!--
                        Main part of the UserControl
                    -->
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>
</DataTemplate>

标签: c#wpfmvvmbinding

解决方案


推荐阅读