0

This is an additional question to my other Thread here: MVVM Navigating through different Views, I've figured out, that the problem no one solved so far (other thread). This question is related to my command's Predicate(CanExecute Method).

public RelayCommand ChangePageCommand {
        get {
            return new RelayCommand(p => ChangeViewModel((BaseViewModel)p), x => x is BaseViewModel);
        }
    }

    public RelayCommand TestChangePageCommand {
        get {
            return new RelayCommand(p => MessageBox.Show((p is BaseViewModel).ToString()), x => true);
        }
    }

I've created some sort of Test Method to determine where the actual problem is located:

MessageBox.Show((p is BaseViewModel).ToString()) 

from TestChangePageCommand shows a Dialogbox with "true" but the Predicate from the ChangePageCommand

x => x is BaseViewModel

always returns "false" (also the Predicate from TestChangePageCommand does if implemented)

May anyone can tell why my App behaves like this?

Update 1:

The Output Window says:

Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HomePage; DataItem=null; target element is 'Button' (Name=''); target property is 'CommandParameter' (type 'Object')

Update 2:

I've checked the ViewModel and found out that HomePage is NOT null when it get's set as the CurrentPageViewModel. Is it possible that the CanExecute Method gets executed when the ViewModel is created and the HomePage is not yet defined? How could I solve this?

UserAdministrationViewModel which contains the HomeViewModel (as HomePage)

5
  • 1
    Put the breakpoint onto x => x is BaseViewModel, and tell us, what is x. Also, don't write read-only properties like you did - your getter produces new RelayCommand instances again and again. Create command once and cache the value in private field.
    – Dennis
    Apr 12, 2016 at 6:15
  • can you test launching a messagebox from canexecute also by doing x.ToString(); to know what actually x is getting passed or is it even being called
    – Nitin
    Apr 12, 2016 at 6:18
  • Somehow x is null when the CanExecute Method is executed - but as mentioned, if I modify the CanExecute Method to just return true and click the Button it'll return "HomeViewModel" which derives from BaseViewModel. After changing some settings the output windows says "'HomePage' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet."
    – Th1sD0t
    Apr 12, 2016 at 6:26
  • can you try raising {RaiseCanExecuteChanged()} on your command once your page is loaded. I suspect somehow canexecute is being called before your page is ready and after that it is not evaluating
    – Nitin
    Apr 12, 2016 at 7:03
  • @Nitin as the app is ViewModel driven this should not be the problem (I think?) because the ViewModel and all it's data should be there before the page gets loaded - or am I wrong?
    – Th1sD0t
    Apr 12, 2016 at 7:07

2 Answers 2

1

As Dennis mentioned above in comments you should try to cashe your command. Pleas try the next thing, put the breakpoint at the predicate line, and see what you get:

    ICommand _command;
    public RelayCommand ChangePageCommand
    {
        get
        {
            return _command ?? (_command = new RelayCommand(p => ChangeViewModel((BaseViewModel) p), x =>
            {
                x is BaseViewModel;
            }));
        }
    }

Try to change the next method:

    public void ChangeViewModel(BaseViewModel viewModel)
    {
        //You should implement clone in your base view model
        CurrentPageViewModel = viewModel.Clone();
    }

Update #2 - Here is your code

<Button Content="{Binding Name}" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
            CommandParameter="{Binding HomePage}"/>

I think you should add the UpdateSourseTrigger=PropertyChanged to your Button CommandParameter definition. As I can understand the HomePage can be null only in time when there is no any Ancestor of type window there. This will happened only when you navigate between two DataTemplates(in your case). Try to think in this approach.

Regards,

5
  • Nice to know, thank you! But as mentioned, somehow x is null when in Predicate and of Type HomeViewModel when clicking on the Button (if I modify the Predicate to just return true)
    – Th1sD0t
    Apr 12, 2016 at 6:46
  • Yep, i've cached the command - still the same problem. I don't get it - why is HomePage "null" when CanExecute is called but of Type HomeViewModel when Execute get's called...
    – Th1sD0t
    Apr 12, 2016 at 6:51
  • I tried your code but the problem still persists. In addition, after setting a Breakpoint inside the setter of HomePage I noticed that it never get's set to null.
    – Th1sD0t
    Apr 12, 2016 at 8:00
  • I already tried to add the UpdateSourceTrigger with no success - CanExecute still returns false because it thinks HomePage is null. There should always be an Ancestor of Type Window because all SubViews (which are UserControls) are hosted inside of it (even the ChangePageCommand is inside the Window's ViewModel and get's called successfully).
    – Th1sD0t
    Apr 12, 2016 at 8:26
  • I've added some more Info to the Question - maybe this helps you to help me.
    – Th1sD0t
    Apr 12, 2016 at 9:16
0

In the end giving my UserControl a Name and redirect the CommandProperty along the ElementName did the trick:

<Button Content="{Binding Name}" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
        CommandParameter="{Binding DataContext.HomePage, ElementName=Test}"/>

Could anyone explain why Binding to the Name (which is located in the same Base class) works without the ElementName / giving additional Information but the HomePage doesn't?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.