1

I feel bad posting this because I see a ton of similar posts, but after going through them all I can't quite diagnose my issue still. What I have is a WPF app designed with the MVVM pattern and using a RelayCommand() implementation for commands.

In my user control's XAML I set the data context here :

<UserControl.DataContext>
    <viewModel:SidePanelViewModel />
</UserControl.DataContext>

Then further down in the XAML I have this snippet where I assign a button's command

<TextBlock FontWeight="Bold" Margin="0,0,0,10">Service List</TextBlock>
<ListBox MaxHeight="100"
        ItemsSource="{Binding ServiceList}"
        SelectedItem="{Binding ServiceToRemove}">
</ListBox>
<Button HorizontalAlignment="Left" Width="60" Margin="0,10"
        Command="{Binding RemoveServiceCommand}">Remove</Button>

I am binding the button to the Command RemoveApplicationCommand which I define in the SidePanelViewModel here :

public ICommand RemoveServiceCommand
{
    get { return new RelayCommand(RemoveService, CanRemoveService); }
}

private void RemoveService()
{
    ServerList.Remove(ServiceToRemove);
}

private bool CanRemoveService()
{
    return true;
}

The problem

If I debug, the getter for RemoveServiceCommand will be reached when the button starts up, but when I click the button the code doesn't reach it. I had a very similar implementation (or so I think) working before, so this is really puzzling me. How can I get the command to fire on click?

CBrown77
  • 517
  • 4
  • 18
  • don't `return new` in the getter, instead return a backing field, `return this._remoteServiceCommand;` or the like. – Federico Berasategui Aug 17 '15 at 21:09
  • @HighCore would this prevent the getter from not being fired though? – CBrown77 Aug 17 '15 at 21:13
  • So, if you are certain that your binding is correct, then it could be a problem with RelayCommand. Can you post your RelayCommand implementation? – Silas Reinagel Aug 17 '15 at 21:40
  • I am not certain. It seems like the binding would more likely be the issue because if I place a breakpoint in the getter for `RemoveServiceCommand` then it will not be hit when I click the button. So the code isn't even getting the chance to run RelayCommand(), right? – CBrown77 Aug 17 '15 at 21:43
  • Check your window `DataContext` at run-time by putting a button in your `xaml` and use a `Breakpoint` in `OnClick` `eventhandler`... Have you the expected value in the `this.DataContext` there? You can write `this.DataContext` in `Watch` panel when the `breakpoint` has been hit... – Ramin Bateni Aug 17 '15 at 22:50
  • @RAM I just did this and its the SidePanelViewModel as it should be...any thoughts? I'm beyond stumped at this point – CBrown77 Aug 18 '15 at 13:18

4 Answers4

1
Command="{Binding RemoveApplicationCommand}"

Did you mean RemoveServiceCommand?

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
1

Try implementing like this

   private ICommand finishCommand;

        public ICommand FinishCommand
        {
            get
            {
                if (this.finishCommand == null)
                {
                    this.finishCommand = new RelayCommand<object>(this.ExecuteFinishCommand, this.CanExecutFinishCommand);
                }

                return this.finishCommand;
            }
        }

private void ExecuteFinishCommand(object obj)
        {
}
 private bool CanExecutFinishCommand(object obj)
        {
            return true;
        }
Justin CI
  • 2,693
  • 1
  • 16
  • 34
  • I tried this, doesn't prevent the ICommand getter from not being fired at all :/ – CBrown77 Aug 18 '15 at 13:19
  • right click on the binding command in xaml then goto definition. is that point to the exact command you bind? – Justin CI Aug 18 '15 at 13:22
  • @JustinCl yes, it brings me into my SidePanelViewModel where I define my ICommand property `RemoveServiceCommand`. Its so strange because I can set the content of the button to something using binding to the same view model but the command still won't work... – CBrown77 Aug 18 '15 at 13:30
  • does it bring you to the top of the viewmodel or on exact RemoveServiceCommand? – Justin CI Aug 18 '15 at 14:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87282/discussion-between-justin-ci-and-cbrown77). – Justin CI Aug 18 '15 at 14:53
  • err this is embarrassing lol. I somehow hadn't put a breakpoint in the RemoveService method, only in the RemoveServiceCommand getter. Turns out my RemoveService implementation was what was flawed. Thanks for helping out! I realized just before you commented that. Do you know why the debugger doesn't go over the getter code but will go into the RemoveService code? – CBrown77 Aug 18 '15 at 14:58
1

Turns out the debugger was going over RemoveService the entire time but I had not put a breakpoint there. I had a wrong name in my RemoveService implementation ServerList.Remove() should have been ServiceList.Remove(). I assumed the debugger would hit a breakpoint in the RemoveServiceCommand property's getter but it turns out it doesn't hit that when you click the button.

CBrown77
  • 517
  • 4
  • 18
0

You're returning a new RelayCommand in your getting, but not saving / caching the instance. Save it in a member variable.

if (_cmd == null)
   _cmd = new ....

return _cmd;
SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • I just tried this, the getter still doesn't fire, this seems like its a different issue? – CBrown77 Aug 17 '15 at 21:23
  • If the getter isn't firing, that means your DataContext isn't set correctly. Is the ListBox getting populated? Have you tried turning on verbose binding diagnostic output? – SledgeHammer Aug 17 '15 at 21:53
  • the ListBox gets populated correctly and if I set the content of the button to the currently selected item in the ListBox it also works. I am having no other data binding issues in this control which is why this is puzzling for me. :/ No I have not tried that, I can do that. – CBrown77 Aug 17 '15 at 21:58