WPF CommandBinding 不在顶级项目/Window/this 上

发布于 2024-09-06 14:56:36 字数 1538 浏览 8 评论 0原文

我想要的只是不同选项卡上的不同绑定,因此切换选项卡会切换命令可用性。我认为 CommandBindings 就是这样工作的。

但我花了最后的时间试图让这个简单的示例发挥作用。要么是我从根本上误解了(这不会是第一次),要么是出了什么问题。

我将 CommandBinding 添加到 textBoxA 但不添加到 textBoxB。在它们之间移动应启用和禁用设置为相应命令的按钮。

将 CommandBinding 添加到窗口可以很好地启用按钮,但这会破坏特定于项目的 CommandBinding 的全部意义。

使用此 XAML

<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="500">
    <Canvas>
        <Button Canvas.Left="31" Canvas.Top="24" Content="Click Me" Name="button1" Width="100"/>
        <TextBox Canvas.Left="155" Canvas.Top="22" Height="23" Name="textBoxA" Width="120" Text="A" />
        <TextBox Canvas.Left="298" Canvas.Top="22" Height="23" Name="textBoxB" Width="120" Text="B" />
    </Canvas>
</Window>

使用此代码隐藏

public MainWindow()
{
    InitializeComponent();

    button1.Command = ApplicationCommands.Open;

    var _Binding = new CommandBinding(button1.Command);
    textBoxA.CommandBindings.Add(_Binding);
    textBoxB.CommandBindings.Clear(); // nothing bound

    _Binding.CanExecute += (s, e) =>
    {
        e.CanExecute = true;
    };

    _Binding.Executed += (s, e) =>
    {
        MessageBox.Show("Hello");
    };
}

您将看到(如果您尝试此代码)该按钮仍然处于禁用状态,即使您从一个文本框移动到另一个文本框也是如此。 (尽管 textBoxA 应该启用该按钮,因为它实现了按钮的 CommandBinding)。

这应该如何运作?

先感谢您。

All I wanted was different bindings on different tabs, so switching tabs would toggle command availability. I thought CommandBindings worked that way.

But I've spent the last while trying to get this simple sample to work. Either I fundamentally misunderstand (and that would not be a first) or something's wrong.

I add a CommandBinding to textBoxA but NOT to textBoxB. Moving between them should enable and disable the button which is set to the corresponding command.

Adding the CommandBinding to the Window enables the button just fine, but that kind of kills the whole point of items-specific CommandBindings.

Using this XAML

<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="500">
    <Canvas>
        <Button Canvas.Left="31" Canvas.Top="24" Content="Click Me" Name="button1" Width="100"/>
        <TextBox Canvas.Left="155" Canvas.Top="22" Height="23" Name="textBoxA" Width="120" Text="A" />
        <TextBox Canvas.Left="298" Canvas.Top="22" Height="23" Name="textBoxB" Width="120" Text="B" />
    </Canvas>
</Window>

Using this Code Behind

public MainWindow()
{
    InitializeComponent();

    button1.Command = ApplicationCommands.Open;

    var _Binding = new CommandBinding(button1.Command);
    textBoxA.CommandBindings.Add(_Binding);
    textBoxB.CommandBindings.Clear(); // nothing bound

    _Binding.CanExecute += (s, e) =>
    {
        e.CanExecute = true;
    };

    _Binding.Executed += (s, e) =>
    {
        MessageBox.Show("Hello");
    };
}

You'll see (if you try this code) that the button remains disabled, even as you move from one textbox to the other. (Even though textBoxA should enable the button because it implementes the button's CommandBinding).

How is this supposed to work?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几味少女 2024-09-13 14:56:36

我看不出这是如何工作的,看来您错过了这一点(抱歉) - CommandBindings 应该应用于父元素(将其放在画布上也可以)。单击按钮时,RoatedCommand 将在可视化树中“冒泡”,这意味着任何父元素都将触发已绑定到该命令的事件。该按钮保持禁用状态的原因是您无法在此时执行打开命令,并且 CommandBinding 不会被评估,因为它不在执行路径中。请参阅此处:

http://msdn.microsoft.com /en-us/library/system.windows.input.commandbinding.aspx

I cant see how this would work and it seems your missing the point (sorry) - CommandBindings are supposed to be applied to parent elements (putting it on the canvas will work as well). When the button is clicked then the RoutedCommand will 'bubble up' through the visual tree which means that any parent elements will have their events that have been bound to this command fired. The reason the button stays disabled is because you cant execute the open command at that point and the CommandBinding is not being evaluated as it is not in the execution path. see here:

http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx

伊面 2024-09-13 14:56:36

我将尝试看看是否可以满足您的场景:

<Canvas>
    <Button ... Command="Open">
        <CommandBindings>
            <CommandBinding Command="Open" Executed="OnOpen"/>
        <CommandBindings>
    </Button>
    <TextBox ... >
        <CommandBindings>
            <CommandBinding Command="Open" Executed="OnOpen"/>
        <CommandBindings>
    </TextBox>
    <TextBox ... />
</Canvas>

在代码隐藏中:

private void OnOpen(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Hello");
}

CommandBindings 的想法是成为在上下文中跨按钮和其他控件(通常是用户控件)进行常见击键工作的快捷方式。然后,每个区域对于打开命令可以有不同的含义(一个区域可能会打开一个文件,另一个区域会打开在 ListView 中选择的项目,等等)。

在我的示例中 - 当文本框 A 或按钮具有焦点时按下 CTRL+O 将触发“Hello”消息框。但请阅读 Leom 链接的页面以获得更深入的解释。

I'll try to see if I can hit your scenario:

<Canvas>
    <Button ... Command="Open">
        <CommandBindings>
            <CommandBinding Command="Open" Executed="OnOpen"/>
        <CommandBindings>
    </Button>
    <TextBox ... >
        <CommandBindings>
            <CommandBinding Command="Open" Executed="OnOpen"/>
        <CommandBindings>
    </TextBox>
    <TextBox ... />
</Canvas>

And in code-behind:

private void OnOpen(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Hello");
}

The idea for CommandBindings is to be a short-cut for having common key-strokes work across buttons and other controls within a context (typically a usercontrol). Then each region can have different meanings for the Open-command (one will perhaps open a file - in another it will open the item selected in a ListView and so on).

In my example - hitting CTRL+O while either TextBox A or the button has the focus will trigger the 'Hello' MessageBox. But read the page Leom linked to for a deeper explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文