Search code examples
wpficommandcommandbinding

WPF CommandBinding NOT on the top level item/Window/this


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.


Solution

  • MSFT gave me the correct answer in their WPF forum, here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bb3d1eb1-96fa-4fbc-beda-799613acb9f7