WPF CommandParameter binding not updating
Asked Answered
M

1

9

I am trying to use Command and CommandParameter binding with Buttons in a WPF application. I have this exact same code working just fine in Silverlight so I am wondering what I have done wrong!

I have a combo box and a button, where the command parameter is bound to the combobox SelectedItem:

<Window x:Class="WPFCommandBindingProblem.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">
    <StackPanel Orientation="Horizontal">
        <ComboBox x:Name="combo" VerticalAlignment="Top" />
        <Button Content="Do Something" Command="{Binding Path=TestCommand}"
                CommandParameter="{Binding Path=SelectedItem, ElementName=combo}"
                VerticalAlignment="Top"/>        
    </StackPanel>
</Window>

The code behind is as follows:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        combo.ItemsSource = new List<string>(){
            "One", "Two", "Three", "Four", "Five"
        };

        this.DataContext = this;

    }

    public TestCommand TestCommand
    {
        get
        {
            return new TestCommand();
        }
    }

}

public class TestCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return parameter is string && (string)parameter != "Two";
    }

    public void Execute(object parameter)
    {
        MessageBox.Show(parameter as string);
    }

    public event EventHandler CanExecuteChanged;

}

With my Silverlight application, as the SelectedItem of the combobox changes, the CommandParameter binding causes the CanExecute method for my command to be re-evaluated with the currently selected item and the button enabled state is updated accordingly.

With WPF, for some reason, the CanExecute method is only invoked when the binding is created when the XAML is parsed.

Any ideas?

Motorist answered 22/6, 2010 at 10:48 Comment(0)
I
10

You need to tell WPF that CanExecute can change - you can do this automatically in your TestCommand class like this:

public event EventHandler CanExecuteChanged
{
    add{CommandManager.RequerySuggested += value;}
    remove{CommandManager.RequerySuggested -= value;}
}

WPF will then ask CanExecute everytime a property changes in the view.

Ichang answered 22/6, 2010 at 12:5 Comment(2)
this will reevaluate all commands for all properties on the UI that change? and no way to use it with Delegate commands? Is there no simpler or ootb solution for this like in silverlight?Levins
This is just the simplest way - you control when to call the event CanExecuteChanged - here I just set it to update whenever the framework decides it may have updated.Ichang

© 2022 - 2024 — McMap. All rights reserved.