首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF CommandBinding不工作

WPF CommandBinding不工作
EN

Stack Overflow用户
提问于 2012-12-04 17:57:16
回答 1查看 4.4K关注 0票数 0

我在WPF窗口有个按钮。我想通过按左键和点击Ctrl+F来显示一条消息,我想要XAML中的大部分代码。代码如下所示。我的问题是,鼠标点击是为我工作,但不是按下键。有人能帮帮我吗。提前谢谢。

代码语言:javascript
复制
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");
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-12-04 19:36:57

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

代码语言:javascript
复制
<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>

更新

要根据不同的按钮单击有不同的行为,这可以通过几种方式来完成。这里有两个:

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

代码语言:javascript
复制
<Button Command="ApplicationCommands.Find" Content="Button1" />
<Button Command="ApplicationCommands.Print" Content="Button2" />

使用CommandParameter

代码语言:javascript
复制
<Button Command="ApplicationCommands.Find" CommandParameter="Find1" Content="Button1" />
<Button Command="ApplicationCommands.Find" CommandParameter="Find2" Content="Button2" />

对于KeyBinding:

代码语言:javascript
复制
<KeyBinding Command="ApplicationCommands.Find" CommandParameter="Find3" />

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

代码语言:javascript
复制
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
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13709001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档