1

I've got problem triggering events in a control template which is in another control template and using MVVM light relay command.

Here is the case: main_control is templated in a generic.xaml style. sub_control is templated the same way.

main_control has several sub_control instance in an ObservableCollection

Now in my SL project implemented with MVVM Light I have a view with a main_control in the xaml. This view is binded to a viewmodel wihch fill it with sub_control.

What I am trying to achieve is to get back in the viewmodel the instance of a sub_control clicked.

Thanks for your help.

SomFred
  • 91
  • 1
  • 6
  • It would be good to attach your codes. Because DataContext and its relationship with VM is the key to solve your problem. – Youngjae Oct 28 '11 at 02:11

1 Answers1

0

Ensure the following things are correct:

Expose the RelayCommand as a property on the object which represents the DataContext of sub_control. This may be your ViewModel, more often it's an instance of an entity class that you are binding to a collection of. Make sure you make it public and provide get and set methods.

  public RelayCommand MyCommand { get; set; }

Make sure the command is set to a new instance

  MyCommand = new RelayCommand(() =>
  {
     // Do something
  });

Finally set the binding in your view

  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
  ...
  <Button Content="Click Me">
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="Click">
        <command:EventToCommand Command="{Binding MyCommand}"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
  </Button>
Dan Wray
  • 689
  • 1
  • 5
  • 17
  • Thx for this answer. I've tried that but doesn't work. The problem is that I want the instance of sub_control clicked backed up to the view model. My sub_control wrap a ui control (Arc) which I can't herit. – SomFred Oct 28 '11 at 08:33
  • I would check the data context of sub_control, if this is in a databound list control or something similar the data context is likely to be an object? In which case you'll either need to add the RelayCommand property to that object or explicitly set the data context of your control template to the instance of the view model. As someone else has commented - code examples would probably help here. – Dan Wray Oct 28 '11 at 15:31