0

Like described in the title, i'd like to link 2 users control. one is a list and an other one is detail of list. I'd like to refresh the detail user control depending on the row selected in my first user control

Are there a way to do this?

user1898765
  • 323
  • 1
  • 6
  • 18
  • 1
    Look at my answer here: http://stackoverflow.com/questions/24682211/wpf-mvvm-communication-between-two-user-controls/24685563#24685563. Instead of executing a command like in my example, you'll just raise a SelectionChanged event in one ViewModel and pass along the new selected item to the other ViewModel. – Lee O. Jul 30 '14 at 14:01
  • 1
    You can also use something like the MVVM Light Messenger...http://www.codeproject.com/Tips/696340/Thinking-in-MVVMLight-Messenger. Send the message in one ViewModel and have the other ViewModel registered to receive those messages. – Lee O. Jul 30 '14 at 14:04

2 Answers2

0

You could have a DependencyProperty on the "List" control which exposes the selected item. Then bind this as the DataContext of the second control.

public YourType SelectedItem
{
  get { return (YourType)this.GetValue(SelectedItemProperty); }
  set { this.SetValue(SelectedItemProperty, value); } 
}
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem", typeof(YourType), typeof(YourListControl));

WPF something like:

<local:YourListControl SelectedItem={Binding SelectedRow, Mode=TwoWay}/>
<local:DetailControl DataContext={Binding SelectedRow}/>

Where SelectedRow would be on your viewmodel

Martin Grundy
  • 254
  • 2
  • 11
  • my usercontrols are in contentcontroller in my ViewPage so my binding is in usercontrol or in view? what is youType type? – user1898765 Jul 31 '14 at 08:59
  • The YourType is the type that represents a row in your list. You have a viewmodel which contains a list of YourType and a property SelectedRow of type YourType. – Martin Grundy Jul 31 '14 at 15:59
-1

i'm really disapointed with your solutions. it's difficult for me to understand how it communicates between both usercontrol. throw the viewmodel containing the usercontrol or the view?

Do you have a minimal sample that i could learn and test for my case?

user1898765
  • 323
  • 1
  • 6
  • 18