Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能在一个';s也用作键绑定?_C#_Wpf_Textbox_Key Bindings_Inputbinding - Fatal编程技术网

C# 我怎样才能在一个';s也用作键绑定?

C# 我怎样才能在一个';s也用作键绑定?,c#,wpf,textbox,key-bindings,inputbinding,C#,Wpf,Textbox,Key Bindings,Inputbinding,我有一个关键时段的全局输入绑定()。我仍然希望能够在文本框中输入它?有没有办法做到这一点 这是一个简单的例子。当我在文本框中键入句点时,将执行该命令 XAML: 您可以在KeyBinding中绑定键,并将其值更改为键。当文本框获得焦点时,无: Xaml: <Window x:Class="UnrelatedTests.Case6.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation

我有一个关键时段的全局输入绑定(
)。我仍然希望能够在
文本框中输入它?有没有办法做到这一点

这是一个简单的例子。当我在
文本框中键入句点时,将执行该命令

XAML:


您可以在
KeyBinding
绑定
,并将其值更改为
键。当
文本框
获得焦点时,无

Xaml:

<Window x:Class="UnrelatedTests.Case6.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        GotFocus="Window_GotFocus">
    <Window.InputBindings>
        <KeyBinding Key="{Binding MyKey}" Command="{Binding Command}" />
    </Window.InputBindings>
    <Grid>
        <TextBox/>
    </Grid>
</Window>

所以你只想在文本框没有焦点时键绑定生效?@GlenThomas这是正确的。周围有很多
TextBox
es,有些甚至“隐藏”在
DataGrid
中,所以如果一个是焦点,那么对它们进行监控是相当复杂的。如果只有一个
文本框
,您的方法似乎是可行的。@Onur:您可以使用
窗口_GotFocus
,找出哪个子控件是
路由事件
的原始源<每次窗口上的任何控件对焦时,代码>窗口对焦
都将升高。我已经更新了我的答案。这符合预期。我通过更改修改器而不是键来修改您的解决方案。谢谢
using System;
using System.Windows;
using System.Windows.Input;

namespace UnrelatedTests.Case6
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
        }

        public ICommand Command
        {
            get { return new CommandImpl(); }
        }


        private class CommandImpl : ICommand
        {
            public bool CanExecute(object parameter)
            {
                return true;
            }

            public void Execute(object parameter)
            {
                MessageBox.Show("executed!");
            }

            public event EventHandler CanExecuteChanged;
        }
    }
}
<Window x:Class="UnrelatedTests.Case6.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        GotFocus="Window_GotFocus">
    <Window.InputBindings>
        <KeyBinding Key="{Binding MyKey}" Command="{Binding Command}" />
    </Window.InputBindings>
    <Grid>
        <TextBox/>
    </Grid>
</Window>
    Key _myKey;
    public Key MyKey
    {
        get
        {
            return _myKey;
        }
        set
        {
            _myKey = value;
            OnPropertyChanged("MyKey");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        MyKey = Key.OemPeriod;
    }

    private void Window_GotFocus(object sender, RoutedEventArgs e)
    {
        if (e.OriginalSource is TextBox)
            MyKey = Key.None;
        else
            MyKey = Key.OemPeriod;
    }