I have a tabcontrol which in the first tab loads an WPF datagrid. The rows in the datagrid are
filled with MemberViewModels.
I have managed to get the selected MemberViewModel when the datagrids selectItem is changed, but how can I pass it on to my RibbonViewModel where I want to add it to the TabItems collection with a command? The RibbonViewModel is the ViewModel of my Ribbon. On that ribbon there is a button which adds a new MemberViewModel to the tabItemsCollection, this works fine. However I want to add the selected MemberViewModel from my datagrid to be added as a new tabitem.
Code (Command) in RibbonViewModel which adds a new MemberViewModel to the TabItemsCollection
private void AddSelectedMemberTabItem(object notUsed)
{
_tabViewModel.TabItems.Add(new MemberViewModel{ Header = "Member" }); // OK, this works fine
_tabViewModel.TabItems.Add(SelectedMemberViewModel); // this doesnt work, DP SelectedMemberViewModel is never filled, don't know how to retrieve it from the selected datagrid item.
_addOverview.RaiseCanExecuteChanged();
}
Code to retrieve the selected MemberViewModel from the selected datagriditem (in OverviewViewModel):
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(MemberViewModel), typeof(OverviewViewModel), new UIPropertyMetadata(OnSelectedMemberPropertyChanged));
public MemberViewModel SelectedItem
{
get { return (MemberViewModel)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
private static void OnSelectedMemberPropertyChanged(DependencyObject m, DependencyPropertyChangedEventArgs args)
{
var selectedMember = m.GetValue(SelectedItemProperty) as MemberViewModel;
_ribbonViewModel.SelectedMemberViewModel = selectedMember; // error: cannot access non-static field (_ribbonViewModel) in static contect
}
How can I fill the _ribbonViewModel.SelectedMemberViewModel from the OnSelectedMemberPropertyChanged method on OverviewViewModel? Or am I going the wrong way with this?
This is thet way I had to do it, this way I can pass the selected MemberViewModel from the DataGrid to an other var.