c# - WPF 用户控件,带有按钮和按键绑定(bind)

标签 c# wpf xaml user-controls key-bindings

我正在尝试在 WPF 中实现拨号器。我有一个窗口,里面有一个用户控件。用户控件有很多按钮,但用户也可以使用数字键盘输入数字。

我创建了一个小型示例项目来展示我所处的位置:

MainWindow.xaml

<Window x:Class="wpf_dialer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:wpf_dialer"
        Title="MainWindow" Height="200" Width="525">
  <Window.Resources>
    <local:DialerViewModel x:Key="myViewModel" />
  </Window.Resources>
  <Grid DataContext="{StaticResource myViewModel}">
    <local:Dialer />
  </Grid>
</Window>

Dialer.xaml

<UserControl x:Class="wpf_dialer.Dialer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:wpf_dialer"
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="300"
             d:DataContext="{d:DesignInstance local:DialerViewModel}"
             Loaded="UserControl_Loaded">
  <UserControl.InputBindings>
    <KeyBinding Key="A" Command="{Binding CommandDialValue, Mode=OneTime}" CommandParameter="." />
    <KeyBinding Key="Back" Command="{Binding CommandDialValue, Mode=OneTime}" CommandParameter="Back" />
    <KeyBinding Key="Enter" Command="{Binding  CommandAcceptValue, Mode=OneTime}" CommandParameter="KeyBinding" />
  </UserControl.InputBindings>
  <UniformGrid Columns="1">
    <TextBox IsEnabled="False" Text="{Binding DialedValue, Mode=OneWay}" MinWidth="200" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10"/>
    <Button Content="OK" Margin="60,30" Command="{Binding CommandAcceptValue, Mode=OneTime}" CommandParameter="Button" />
  </UniformGrid>
</UserControl>

DialerViewModel.cs

using System;
using System.ComponentModel;
using System.Windows.Input;

namespace wpf_dialer
{
    class DialerViewModel : INotifyPropertyChanged
    {
        private Random RAND = new Random();
        private string _dialed_value = "00";
        public string DialedValue
        {
            get { return _dialed_value; }
            set
            {
                if (_dialed_value == value) return;
                _dialed_value = value;
                RaisePropertyChanged("DialedValue");
            }
        }

        public ICommand CommandDialValue { get { return new CommandImpl(DialValue); } }
        public ICommand CommandAcceptValue { get { return new CommandImpl(Alert); } }

        private void DialValue(object parameter)
        {
            if (parameter.ToString() == "Back")
            {
                if (DialedValue.Length > 0)
                {
                    DialedValue = DialedValue.Substring(0, DialedValue.Length - 1);
                }
            }
            else
            {
                DialedValue += RAND.Next(0, 10).ToString();
            }
        }

        private void Alert(object parameter)
        {
            System.Windows.MessageBox.Show(parameter.ToString());
        }

        #region INotifyPropertyChanged

        protected void RaisePropertyChanged(string property_name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property_name));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private class CommandImpl : ICommand
        {
            private readonly Action<object> _action = null;
            public CommandImpl(Action<object> action)
            {
                _action = action;
            }

            public event EventHandler CanExecuteChanged;
            public bool CanExecute(object parameter) { return true; }
            public void Execute(object parameter) { _action(parameter); }
        }
    }
}

目标:

  • 一旦窗口加载,当用户按下 A 键时,CommandDialValue 就会被执行;
  • 当用户按 Enter 时,将显示一个消息框,其中包含文本“KeyBinding”。 CommandAcceptValue 必须从 KeyBinding 调用,而不是从按钮调用;

问题:

  • 加载窗口时,KeyBindings 不会执行。当我单击用户控件中某处的按钮时,它们就会被执行;

  • 当我按 Enter 时,会执行按钮的命令,但我希望执行用户控件的 KeyBinding;

  • 此拨号器必须保存在 UserControl(或 ControlTemplate 或 DataTemplate)中,因为它包含在一个非常复杂的窗口中。

  • 我不想将 KeyBindings 放在 Window 上,因为这样 UserControl 就不可重用,而且它的 DataContext 与用户控件不同。

更新:

我通过在所有按钮上设置 Focusable="False" 解决了第二个问题。

最佳答案

为了防止按钮获得焦点,我设置了 Focusable="False"对于所有按钮。

为了在窗口打开时设置焦点,我设置了 Focusable="True"在 UserControl 上,以及在 Loaded 事件上我调用 Focus() .

Dialer.xaml

             d:DataContext="{d:DesignInstance local:DialerViewModel}"
             Loaded="UserControl_Loaded" Focusable="True">
  <UserControl.InputBindings>

Dialer.xaml.cs

private void UserControl_Loaded(object sender, RoutedEventArgs e) {
    Focus();
}

我没有找到 FocusManager.FocusedElement 的组合那行得通。我试过{Binding ElementName=myUserControl} ,和{Binding RelativeSource={RelativeSource Self}} .

关于c# - WPF 用户控件,带有按钮和按键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33167684/

相关文章:

c# - 从 .NET 执行 Cygwin 进程?

c# - Collection.ToList<TResult>() 扩展方法引发编译时错误

c# - 在 ListView 中的网格中的标签中绑定(bind)到 ContextMenu 中的 ICommand 不起作用

c# - 如何使用匿名方法初始化静态只读变量?

c# - 给定对结构的托管引用,如何获取对偏移量字段的托管引用?

c# - WPF 表单与后台线程锁定

c# - 在 WPF 中取消隐藏 Datagrid 列

c# - 在 WPF 中,如何在不使用任何事件的情况下判断鼠标左键当前是否按下?

wpf - 使用交互触发器时获取 EventArgs

c# - 了解 XAML/WPF 中的样式和模板