WPF KeyBinding 命令未在自定义控件中触发

阿德里安·S

我正在构建一个 WPF 自定义控件(注意:不是用户控件)。控件的c#代码定义如下:

using System.Windows;
using System.Windows.Controls;
using MvvmFoundation.Wpf;

namespace TextBoxWithInputBinding
{
    public class AutoComp : Control
    {
        static AutoComp()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoComp), new FrameworkPropertyMetadata(typeof(AutoComp)));
        }

        public AutoComp()
        {
            DownCommand = new RelayCommand(() =>
            {
                System.Diagnostics.Debug.WriteLine("Down");
            });
            PressedCommand = new RelayCommand(() =>
            {
                System.Diagnostics.Debug.WriteLine("Pressed");
            });
        }

        public RelayCommand DownCommand
        {
            get { return (RelayCommand)GetValue(DownCommandProperty); }
            set { SetValue(DownCommandProperty, value); }
        }

        public static readonly DependencyProperty DownCommandProperty =
            DependencyProperty.Register("DownCommand", typeof(RelayCommand), typeof(AutoComp), new PropertyMetadata(null));

        public RelayCommand PressedCommand
        {
            get { return (RelayCommand)GetValue(PressedCommandProperty); }
            set { SetValue(PressedCommandProperty, value); }
        }

        public static readonly DependencyProperty PressedCommandProperty =
            DependencyProperty.Register("PressedCommand", typeof(RelayCommand), typeof(AutoComp), new PropertyMetadata(null));
    }
}

我在Generic.xaml中为控件定义模板,如下所示:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TextBoxWithInputBinding">
    <Style TargetType="{x:Type local:AutoComp}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:AutoComp}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            >
                        <StackPanel>
                            <TextBox>
                                <TextBox.InputBindings>
                                    <KeyBinding Key="Down" Command="{TemplateBinding DownCommand}"/>
                                </TextBox.InputBindings>
                            </TextBox>
                            <Button Content="Press me" Command="{TemplateBinding PressedCommand}"/>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

当我按下“按下我”按钮时,会触发 PressedCommand(输出窗口中出现“按下”一词 - 但是,当我输入文本框并按下向下键时,没有任何反应。

我需要做什么才能使 DownCommand 火起来?

阿德里安·S

所以我找到了答案:我需要常规绑定而不是模板绑定:我替换了

<KeyBinding Key="Down" Command="{TemplateBinding DownCommand}"/>

<KeyBinding Key="Down"
    Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DownCommand}"/>

我仍然想知道为什么 Button 绑定适用于 TemplateBinding 而 TextBox KeyBinding 没有!!!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

存在KeyBinding时未触发WPF KeyTrigger

来自分类Dev

C#WPF在XAML中添加KeyBinding事件

来自分类Dev

在 UWP 中是否有 WPF 的“KeyBinding”的替代方案?

来自分类Dev

鼠标事件未在自定义控件C#WPF上触发

来自分类Dev

keyBinding中的焦点导航

来自分类Dev

WPF自定义控件-绑定到在后台代码中定义的命令

来自分类Dev

继承的WPF自定义控件不继承父命令

来自分类Dev

WPF有条件地启用KeyBinding

来自分类Dev

WPF中的自定义控件模板的Click事件

来自分类Dev

WPF自定义控件中的依赖关系

来自分类Dev

WPF:如何使自定义控件中的文本显示?

来自分类Dev

WPF-更新自定义控件中的值

来自分类Dev

在WPF MaterialDesign中悬停自定义控件

来自分类Dev

WPF:自定义创建的用户控件中的数据绑定

来自分类Dev

自定义 WPF 控件的好处

来自分类Dev

WPF 样式自定义控件

来自分类Dev

在自定义WPF控件中强制重新绘制自定义绘制的UIElement

来自分类Dev

WPF-配置自定义事件以触发用户控件上的开始故事板

来自分类Dev

子控件未继承 WPF 自定义控件样式

来自分类Dev

使用WPF C#中的多个控件的组合创建一个自定义控件

来自分类Dev

自定义控件中的WPF扩展工具包控件无法通过表单生成吗?

来自分类Dev

实施自定义WPF命令

来自分类Dev

WPF MouseDown事件未在控件中到处触发

来自分类Dev

WPF MouseDown事件未在控件中到处触发

来自分类Dev

如何在WPF中的FormattedText的简单自定义控件中设置依赖项属性AddOwner

来自分类Dev

在WPF中的网格中的单元格之间拖放自定义控件

来自分类Dev

将XAML插入编辑器中的WPF自定义控件中

来自分类Dev

wpf-无法使用自定义控件

来自分类Dev

WPF自定义控件,模型作为依赖项属性

Related 相关文章

热门标签

归档