【问题标题】:Why this simple WPF command does not work?为什么这个简单的 WPF 命令不起作用?
【发布时间】:2019-03-25 16:06:35
【问题描述】:

我正在尝试将 WPF 自定义命令添加到控件。我做了什么:

XAML

<Window x:Class="H.I.S.windows.CommandTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:H.I.S.windows"
        mc:Ignorable="d"
        Title="CommandTest" Height="450" Width="800">
    <Window.CommandBindings>
        <CommandBinding Command="local:CustomCommand.Save" CanExecute ="SaveCommand_CanExecute"  Executed="SaveCommand_Executed" />
    </Window.CommandBindings>
    <Grid>
        <Button Command="local:CustomCommand.Save" Height="50" Width="100">Click me!</Button>
    </Grid>
</Window>

C#

namespace H.I.S.windows
{

    public partial class CommandTest : Window
    {
        public CommandTest()
        {
            InitializeComponent();
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Saved");
        }
    }
    public static class CustomCommand
    {
        public static readonly RoutedUICommand Save = new RoutedUICommand(
            "Save",
            "Save",
            typeof(CustomCommand),
            new InputGestureCollection()
                {
                    new KeyGesture(Key.F2)
                }
            );
    }
}

按钮被禁用(即使在设计模式下)并且不允许用户点击它。 我刚刚实现了HERE 描述的代码。 我哪里错了?

【问题讨论】:

  • 该按钮仅在设计模式下被禁用,但在运行时启用并工作。见github.com/SirRufo/so/tree/master/52914821/WpfApp1
  • @SirRufo 我试过你的解决方案和命令在运行时工作但我的不起作用
  • 但它与您发布的 完全一样 - 检查 MainWindow xaml。如果它在您的 real 代码中不起作用,您没有向我们展示您的 real 代码
  • 按钮必须在inside CommandBinding 的定义中(在您的示例中,您为整个窗口定义它)否则您必须为按钮设置 CommandTarget跨度>
  • 我已经用一个测试命令和两个按钮更新了示例项目。由于设置了 CommandTarget,一个不工作,另一个工作

标签: c# wpf command


【解决方案1】:

由于以下语句,您发布的代码向我抛出了错误,

  <Window.CommandBindings>
          <CommandBinding Command="local:CustomCommand.Save" CanExecute ="CommandBinding_CanExecute"  Executed="CommandBinding_Executed" />
  </Window.CommandBindings>

将其更改为以下后,它开始为我工作,

   <Window.CommandBindings>
    <CommandBinding Command="local:CustomCommand.Save" CanExecute ="SaveCommand_CanExecute"  Executed="SaveCommand_Executed" />
   </Window.CommandBindings>

代码隐藏中的事件处理程序与 xaml 中用于 CommandBinding 的事件处理程序不同。

“SaveCommand_CanExecute”和“SaveCommand_Executed”

按上述更改后,它对我有用,当我点击它时,我可以看到带有“已保存”消息的消息框。

希望你不会错过这个。如果有其他问题阻止您,请尝试查看它是否进一步显示任何错误并告诉我们。

【讨论】:

  • 哦,谢谢 G K 问题不是你说的,这只是我在处理代码时的一个错误。我对代码进行了一些更改,以便更容易理解。不幸的是,问题仍然存在。
【解决方案2】:

正如@SirRufo 在问题 cmets 中建议的那样,问题是我为整个 WPF 窗口和另一个控件内的按钮声明了“CommandBindings”。

这种情况有两种解决方案:

1:为 Button 的直接父级声明 CommandBindings。

2:为与命令绑定的控件设置名称,并添加如下代码来控制:

<Window ... x:Name = "windowName" ...>
<Window.CommandBindings>
        <CommandBinding Command="custumCommand:CustomCommands.Save" CanExecute ="CommandBinding_CanExecute"  Executed="CommandBinding_Executed" />
    </Window.CommandBindings>
<Grid>
<StackPanel>
<GroupBox>
<Button Command="custumCommand:CustomCommands.Save" CommandTarget = "{Binding ElementName=windowName}" Content="Save" />
</GroupBox>
</StackPanel>
</Grid>
</Window>

查看Button的“CommandTarget”属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多