WPF CommandBinding无法正常工作

时间:2012-12-04 17:57:17

标签: wpf commandbinding

我在WPF窗口中有一个按钮。我想通过左键单击鼠标并单击Ctrl + F来显示一条消息。我想要XAML中的大部分代码。代码如下。 我的问题是鼠标点击对我有效,但不是按键。任何人都可以帮助我。提前谢谢。

MyWindow.xaml

<Window x:Class="Commands_Xaml.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,88,0,0" Name="button1" VerticalAlignment="Top" Width="75">
            <Button.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed"/> 
            </Button.CommandBindings>
            <Button.InputBindings>
                <KeyBinding Key="F" Modifiers="Control" Command="ApplicationCommands.Find"/>
                <MouseBinding MouseAction="LeftClick" Command="ApplicationCommands.Find"/>
            </Button.InputBindings>
        </Button>
    </Grid>
</Window>


MainWindow.xaml.cs

namespace Commands_Xaml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
            //ApplicationCommands.Find.InputGestures.Add(new KeyGesture(Key.F,ModifierKeys.Control));
            //ApplicationCommands.Find.InputGestures.Add(new MouseGesture(MouseAction.LeftClick));

            //CommandBinding bindingObject = new CommandBinding();
            //bindingObject.Command = ApplicationCommands.Find;
            //bindingObject.Executed += new ExecutedRoutedEventHandler(CommandBinding_Executed);
            //this.CommandBindings.Add(bindingObject);
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Button clicked");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

将命令绑定/输入绑定移动到窗口级别并使用按钮的Command属性,而不是尝试使用MouseBinding。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed" />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Command="ApplicationCommands.Find" Key="F" Modifiers="Control" />
    </Window.InputBindings>

    <Grid>
        <Button Command="ApplicationCommands.Find" Content="Button" HorizontalAlignment="Left" Margin="206,152,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

<强>更新

要根据不同的按钮点击进行不同的行为,可以通过几种方式实现。这是两个:

为每个按钮使用不同的命令:

<Button Command="ApplicationCommands.Find" Content="Button1" />
<Button Command="ApplicationCommands.Print" Content="Button2" />

使用CommandParameter

<Button Command="ApplicationCommands.Find" CommandParameter="Find1" Content="Button1" />
<Button Command="ApplicationCommands.Find" CommandParameter="Find2" Content="Button2" />

对于KeyBinding:

<KeyBinding Command="ApplicationCommands.Find" CommandParameter="Find3" />

您可以在CommandBinding.Executed处理程序中访问此属性:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Parameter as string == "Find1")
    {
        //Find1
    }
    else if (e.Parameter as string == "Find2")
    {
        //Find2
    }    
    else if (e.Parameter as string == "Find3")
    {
        //Find3
    }
}