views:

1094

answers:

2

Hi,

I've got a VehicleViewModel that has a sub ViewModel of NotesViewModel

public IManageVehicleNotesViewModel NotesViewModel { get; set; }

On the first activation of VehicleViewModel I activate the sub ViewModel.

NotesViewModel.Activate();

The activation calls a method to init a number of Commands, I've break pointed this and its being called.

CreateCommand = new DelegateCommand<object>(OnCreateCommand, CanCreate);

However although the TextBoxes are binding on the sub View (so the DataContext is in place) none of the commands are binding - I've tried to calling RaiseCanExecuteChanged on the commands but they don't disable, enable or call the methods as expected.

I don't know whether its relevant (as the TextBoxes are binding) but I'm associating the View and ViewModel using a ResourceDictionary as so ...

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:v="clr-namespace:GTS.GRS.N3.Modules.Vehicles.Views"
    xmlns:vm="clr-namespace:GTS.GRS.N3.Modules.Vehicles.Model">
  <DataTemplate DataType="{x:Type vm:ManageVehicleViewModel}">
    <v:ManageVehicleView />
  </DataTemplate>
  <DataTemplate DataType="{x:Type vm:ManageVehicleNotesViewModel}">
    <v:ManageVehicleNotesView />
  </DataTemplate>
</ResourceDictionary>

The commands on the top level VehicleViewModel work.

Has anyone experienced anything like this? Is it the order I'm doing things? Any help gratefully received!

Cheers,

Andy

A: 

Do this and check the output to see what is going on:

<UserControl …
     xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase" />

<Button Command="{Binding MyCommand, 
                  diagnostics:PresentationTraceSources.TraceLevel=High}" … />

It should report what object it's actually trying to bind to, etc. Check your output window while you are running to see what is going on with that binding.

Anderson Imes
+2  A: 

Does the CreateCommand property trigger the PropertyChanged event ? If it doesn't, the UI won't be notified when you assign it...

Try to use a tool like Snoop to check whether the Command property of the button is set

Thomas Levesque
Nice one thanks, previously I've never had to have a PropertyChanged on the Commands, but I've just refactered my Modules. I'd got it into my head that RaiseCanExecuteChanged was the alternative to PropertyChanged for Commands, thanks for the pointer!
Andy Clarke
If the command is defined before the View is bound to the ViewModel, you don't need to raise PropertyChanged. Anyway, my command properties are usually read-only and lazy-initialized, so I don't need to worry about it
Thomas Levesque