如何将keydown绑定到按钮的点击事件UWP?

时间:2016-01-24 19:02:06

标签: c# uwp

我有一个小的UWP计算器应用程序,我试图弄清楚如何将键盘数字绑定到按钮的点击事件x:Bind。在下面的例子中,我通过了内容" 7"到ViewModel中的一个按钮RoutedEventArgs。理想情况下,我只需将键盘和数字键盘7绑定到XAML中的按钮,而无需单独的事件处理程序。或者我是否需要使用事件处理程序后面的代码将数字绑定到ViewModel的事件处理程序?

<Button x:Name="SevenButton"
        Grid.Row="4"
        Grid.Column="2"
        Content="7"
        Click="{x:Bind ViewModel.OnNumberBtnClick}"></Button>

//ViewModel
public void OnNumberBtnClick(object sender, RoutedEventArgs args)
{
    if ((_outputValue) == "0" || (isOperationPerformed))
        _outputValue = "";

    isOperationPerformed = false;
    Button numberBtnClick = (Button)sender;

    if ((string)numberBtnClick.Content == ".")
    {
        if (!_outputValue.Contains("."))
        {
            OutputValue = OutputValue + numberBtnClick.Content;
        }
    }
    else
    {
        OutputValue = OutputValue + numberBtnClick.Content;
    }
}

//OnPropertyChanged
public string OutputValue
{
    get
    {
        return _outputValue;
    }
    set
    {
        _outputValue = value;
        OnPropertyChanged("OutputValue");
    }
}

谢谢你, 标记

因此,在遵循RareNCool的一些建议之后,这就是我所提出的。还有一些问题要解决,比如专注于文本框,否则它会起作用。

在我的视图中,我刚刚为ViewModel KeyRoutedEventArgs事件添加了另一个Bind:

        <TextBlock x:Name="OutputValueTextBlock"
                   IsTextSelectionEnabled="True"
                   KeyDown="{x:Bind ViewModel.OnKeyboardNumberInput, Mode=OneWay}"
                   Text="{x:Bind ViewModel.OutputValue, Mode=TwoWay}"></TextBlock>     

在ViewModel中我创建了这个事件:

  public void OnKeyboardNumberInput(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Number0 || e.Key == VirtualKey.NumberPad0 || (isOperationPerformed))
            _outputValue = "";

        if ((e.Key > VirtualKey.Number0 || e.Key < VirtualKey.Number9) || (e.Key > VirtualKey.NumberPad0 || e.Key < VirtualKey.NumberPad9))
        {
            string numberKey = e.Key.ToString();

            if (e.Key == VirtualKey.Decimal)
            {
                isOperationPerformed = false;

                if (!_outputValue.Contains("."))
                {
                    // Switch if E.Key is a decimal and _output does not contains a decimal.
                    switch (numberKey)
                    {
                        case "Decimal":
                            numberKey = ".";
                            OutputValue = OutputValue + numberKey;
                            break;
                    }
                }
            }
            else
            {
                // Swtich if E.Key is not a decimal.
                switch (numberKey)
                {
                    case "Number0":
                    case "NumberPad0":
                        numberKey = "0";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number1":
                    case "NumberPad1":
                        numberKey = "1";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number2":
                    case "NumberPad2":
                        numberKey = "2";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number3":
                    case "NumberPad3":
                        numberKey = "3";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number4":
                    case "NumberPad4":
                        numberKey = "4";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number5":
                    case "NumberPad5":
                        numberKey = "5";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number6":
                    case "NumberPad6":
                        numberKey = "6";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number7":
                    case "NumberPad7":
                        numberKey = "7";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number8":
                    case "NumberPad8":
                        numberKey = "8";
                        OutputValue = OutputValue + numberKey;
                        break;
                    case "Number9":
                    case "NumberPad9":
                        numberKey = "9";
                        OutputValue = OutputValue + numberKey;
                        break;
                }
            }

        }
    }

1 个答案:

答案 0 :(得分:2)

我将实际处理放入带有string参数的单独函数中,然后捕获Button Click和Page KeyDown事件并传入其内容。但是,如果您真的只想拥有一个函数,则可以将ClickKeyDown事件绑定到函数,并将两个参数都键入为objects,然后使用typeof确定你需要从中得到什么。