简体   繁体   English

WPF XAML网格键绑定似乎不起作用

[英]WPF XAML Grid- KeyBinding doesn't seem to work

I am trying to add the functionality to allow the user to 'zoom in' on a web page displayed in a ChromiumWebBrowser on the GUI of my WPF application, by using the keyboard. 我正在尝试添加功能,以允许用户使用键盘“放大”在WPF应用程序的GUI上的ChromiumWebBrowser显示的网页上。

I have the following function in the code-behind for my XAML: 我的XAML的代码背后有以下功能:

private void zoomInExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        Console.WriteLine("'zoomInExecuted() called. ");
        browser.ZoomLevel++;
    } 

To enable this function to be called, I have added the following <Grid.InputBindings> tags to the <Grid> that I'm using to display the ChromiumWebBrowser : 为了使该函数能够被调用,我向用于显示ChromiumWebBrowser<Grid>添加了以下<Grid.InputBindings>标记:

 <Grid x:Name="grdBrowserHost" MinHeight="900" Height="Auto" MinWidth="1205" Width="Auto" Margin="5,0,0,0" DockPanel.Dock="Bottom" Grid.ColumnSpan="1" >
    <Grid.InputBindings>
        <KeyBinding Key="Add" Command="{Binding Path=zoomInExecuted}"></KeyBinding>
     </Grid.InputBindings>
    ...
</Grid>

As I understand, this should mean that the zoomInExecuted(...) function should be called when the + button is pressed on the keyboard, when the Grid displaying the browser has focus. 据我了解,这意味着当显示浏览器的网格具有焦点时,应在按下键盘上的+按钮时调用zoomInExecuted(...)函数。

But, when I run my application, and click inside the browser to ensure it has focus, if I then press '+' on the keyboard, nothing happens, and I'm not even seeing the debug from my zoomInExecuted() function in the console, so it seems that pressing the '+' key is not actually calling that function. 但是,当我运行我的应用程序并在浏览器中单击以确保它具有焦点时,如果再按键盘上的“ +”号,则什么也不会发生,并且什至看不到我的zoomInExecuted()函数中的调试信息。控制台,因此按“ +”键实际上并不是在调用该函数。 Have I done the KeyBinding correctly? 我是否正确完成了KeyBinding Is there something I'm missing from my code here? 这里的代码中我缺少什么吗?

Edit 编辑

I have tried using an ICommand , as suggested in the answers: 我已经尝试使用ICommand ,如答案所示:

public ICommand zoomInCommand
    {
        get
        {
            _zoomIn = new DelegateCommand(zoomInExecuted()); //CallZoomIn());
            return zoomIn;
        }
    }

and calling this in the KeyBinding in the XAML: 并在XAML的KeyBinding中调用此方法:

<KeyBinding Key="Add" Command="{Binding Path=zoomInCommand}"></KeyBinding>

but I'm getting a compile error in the C# which says: 但是我在C#中遇到编译错误,该错误是:

The type or namespace name 'DelegateCommand' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ DelegateCommand”(您是否缺少using指令或程序集引用?)

Do I need to add a particular reference or using statement in order to be able to use this? 我是否需要添加特定的引用或using语句才能使用它?

Edit 编辑

I have also tried adding the <KeyBinding ...> tags to both the <Grid> that's holding the browser object, and the browser itself in the XAML, ie 我还尝试将<KeyBinding ...>标记添加到保存浏览器对象的<Grid>和XAML中的浏览器本身,即

<Grid x:Name="grdBrowserHost" MinHeight="900" Height="Auto" MinWidth="1205" Width="Auto" Margin="5,0,0,0" DockPanel.Dock="Bottom" Grid.ColumnSpan="1" >
    <Grid.InputBindings>
        <KeyBinding Modifiers="Ctrl" Key="Add" Command="{Binding zoomInExecuted}"></KeyBinding>
    </Grid.InputBindings>
    ...
    <cefSharp:ChromiumWebBrowser Name="browser" Height="Auto" Width="Auto" Grid.Row="0" Address="https://web.riviam.com" Margin="25,35,-0.2,0" >
        <cefSharp:ChromiumWebBrowser.InputBindings>
            <KeyBinding Modifiers="Ctrl" Key="Add" Command="{Binding zoomInExecuted}"></KeyBinding>
        </cefSharp:ChromiumWebBrowser.InputBindings>
    </cefSharp:ChromiumWebBrowser>

But the zoomInExecuted(...) function never appears to be called (I never see the debug from this function displayed in the console)- it seems that pressing CTRL+ on the keyboard is never registered by the application... 但是zoomInExecuted(...)函数似乎从未被调用过(我从没有在控制台中显示此函数的调试信息)-似乎应用程序从未注册过按键盘上的CTRL+

Is there an EventHandler / KeyboardListener or something similar that I need to add to the application? 是否有EventHandler / KeyboardListener或类似的东西需要添加到应用程序中?

You cannot bind to a function, you must use a Command : 您无法绑定到函数,必须使用Command

<KeyBinding Key="Add" Command="{Binding ZoomInCommand}"></KeyBinding>

public ICommand ZoomInCommand
{
    get
    {
        _zoomIn = new DelegateCommand(CallZoomIn());
        return zoomIn;
    }
}

DelegateCommand is part of Microsoft.Practices.Prism . DelegateCommandMicrosoft.Practices.Prism的一部分。 You can download it here 您可以在这里下载

Most MVVM frameworks include it also. 大多数MVVM框架也包括它。

Missing the Icommand to method linking part. 缺少Icommand到方法链接部分。 Else all is correct. 其他都是正确的。 To execute method you need to have property of type ICommand. 要执行方法,您需要具有ICommand类型的属性。 And then use that property to invoke the method you want. 然后使用该属性调用所需的方法。 Please see the link. 请查看链接。 There is relay command created which can be used as generic command in which you can pass your method which you want to execute 创建了中继命令,可以将其用作通用命令,在其中可以传递要执行的方法

Binding Button click to a method 绑定按钮单击方法

You cant bind a function. 您无法绑定功能。 You need to create an ICommand for the purpose of Binding. 您需要创建一个ICommand来进行绑定。

Assuming that you have Implemented MVVM Pattern across the app , a RelayCommand needs to be implemented. 假设您已在整个应用程序中实现了MVVM模式, 需要实现RelayCommand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM