0

tl;dr: Changed Windows to two Pages and a NavigationWindow, ICommand Bindings stopped working.

So I copied the Grid from my MainWindow to a fresh MainPage, setup the NavigationWindow and so on. All is set, build without error, even the Binding for Strings and others are working. What does not work, are the ICommand Bindings for my Buttons. But they did work before.

Example ICommand:

class ComPortChosen : ICommand
{
    private MainViewModel mainVm;
    public ComPortChosen(MainViewModel mainVm)
    {
        this.mainVm = mainVm;
    }
    bool ICommand.CanExecute(object parameter)
    {
            return true;
    }

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

    void ICommand.Execute(object parameter)
    {
        //set a breakpoint here, method was **not** entered
        int i = 1337;
    }
}

In the MainViewModel.class:

public ICommand chooseComPort { get; set; }
private MainPage mainPage;

public MainViewModel(MainView view)
{
    chooseComPort = new ComPortChosen(this);

    mainPage = new MainPage(this);
}

In my MainPage.xaml:

 <Button 
     x:Name="btn_choose" 
     Content="Choose" Grid.Column="10" Grid.Row="2" 
     Command="{Binding chooseComPort}" Margin="0,49,0,0"/>

Since the other Bindings (strings, bool, custom converter) do work, I assume that I set up the DataContext correctly.

yennsarah
  • 5,467
  • 2
  • 27
  • 48
  • have you tried using a program such as snoop to find what the datacontext is set to at runtime? You could also put a break point in the Constructor to see what the datacontext is when the new page is loaded – Ben Steele Jun 30 '16 at 09:24
  • `DataContext` is the set `MainViewModel`, since I set it in the constructor. :/ I haven't worked with such a program.. – yennsarah Jun 30 '16 at 09:28
  • What does `Output` window says when you click at the Button? To see `Output` Window run your program by `F5`. – StepUp Jun 30 '16 at 09:40
  • In addition, try to write like this: `Command="{Binding DataContext.chooseComPort}"`. What is `this` inside of `chooseComPort = new ComPortChosen(this);`. Instead of this you should write a method which will be invoked when button is clicked. – StepUp Jun 30 '16 at 09:41
  • There is nothing in the `Output` when I press the `Button`. @StepUp. I have tried to add the `DataContext` like that, unfortunately, it did not work. `this` in the constructor is the `MainViewModel` instance. The method, which will be executed is in the `ComPortChosen.class` (`void ICommand.Execute(object parameter)`). – yennsarah Jun 30 '16 at 09:48
  • 1
    How to debug binding errors http://www.wpf-tutorial.com/data-binding/debugging/ What you should write instead of `this` in `Command` handler: http://stackoverflow.com/questions/12422945/how-to-bind-wpf-button-to-a-command-in-viewmodelbase – StepUp Jun 30 '16 at 09:55
  • Debug with `Command="{Binding chooseComPort, diag:PresentationTraceSources.TraceLevel=High}"`seems fine, no errors or suspicious lines. Passing the method to the `Command`/making the `Command handler` a property handler solved my problem. Thank you. – yennsarah Jun 30 '16 at 10:08

0 Answers0