Debdeep
Debdeep

Reputation: 241

WPF Command not Enabling a button as expected

I have simulated the scenario with a simple example where window has one textbox and a buton beside it. The button gets activated after the value on textbox goes above 10000. But the button is not getting enabled.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="150" Width="225">
<Grid>
    <WrapPanel>
        <TextBox Text="{Binding X}" Width="100"/>
        <Button Command="{Binding ButtonCommand}" CommandParameter="{Binding}" Width="100"/>
    </WrapPanel>
</Grid>

    public partial class MainWindow : Window
{
    private ViewModel vm = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = vm;
    }

    protected override void OnContentRendered(EventArgs e)
    {
        Task.Run(new Action(() =>
        {
            int c = 0;
            while (true)
            {
                vm.X = c++;
            }
        }));

        base.OnContentRendered(e);
    }
}

public class ViewModel : INotifyPropertyChanged
{
    int x;
    public int X
    {
        get { return x; }
        set
        {
            if (x != value)
            {
                x = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("X"));
                }
            }
        }
    }
    ICommand c = new MyCommand();
    public ICommand ButtonCommand
    {
        get
        {
            return c;
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
public class MyCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        if (parameter != null && (parameter as ViewModel).X > 10000)
        {
            return true;
        }
        return false;
    }
    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }

        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }
    public void Execute(object parameter)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 0

Views: 1295

Answers (3)

aqwert
aqwert

Reputation: 10789

You need to have the following...

while (true)
{
     vm.X = c++;
     CommandManager.InvalidateRequerySuggested();
}

Upvotes: 1

JWP
JWP

Reputation: 6963

Here's a simple way to implement the ICommand

public class MyCommand : ICommand
{
  private bool _CanExecute = true;
  public bool CanExecute(object parameter)
  {

    return _CanExecute;
  }

  public void Execute(object parameter)
  {
    if(parameter!=null){
      _CanExecute = false;
          //do your thing here....
     _CanExecute = true; 
   }
}

Purists won't like this pattern but... who cares about all the non-sense of hooking up an tearing down event handlers? Bottom line is the command can be executed or not regardless of re-query suggested.

Upvotes: 0

Bakri Bitar
Bakri Bitar

Reputation: 1697

You have to raise the event CanExecuteChanged at any point you expect the CanExecute method output will be changed

so for example you can add

CanExecuteChanged ();
     vm.X = c++;

Upvotes: 0

Related Questions