I am developing WPF application with Prism MVVM framework. And I doesn't know how properly pass data between parent and child view models.
I have 2 view models - ParentViewModel and inner ChildViewModel.
public class ParentViewModel
{
public ParentViewModel
{
ChildViewModel = new ChildViewModel(params);
}
private ChildViewModel _childViewModel;
public ChildViewModel ChildViewModel
{
get { return _childViewModel; }
set
{
SetProperty(ref _childViewModel, value);
}
}
//This is does not work
public int SelectedChildNumber
{
return _childViewModel.SelectedNumber;
}
}
public class ChildViewModel
{
public ChildViewModel
{
_numbers = new List<int>();
}
private List<int> _numbers;
public List<int> Numbers
{
get { return _numbers; }
set
{
SetProperty(ref _numbers, value);
}
}
private int _selectedNumber;
public int SelectedNumber
{
get { return _selectedNumber; }
set
{
SetProperty(ref _selectedNumber, value);
}
}
}
I want to get and use selected value from child view model. My approach doesn't work - SelectedChildNumber doesn't want to refresh if SelectedNumber changes in ChildViewModel.
UPDATE: Ok, What if I have ChildViewModel collection in ParentViewModel. One of this ChildViewModels have property IsSelected equals true. How to get this one selected view model from collection?
public class ParentViewModel
{
public ParentViewModel
{
Items = GetItems();
}
private ObservableCollection<ChildViewModel> _items;
public ObservableCollection<ChildViewModel> Items
{
get
{
return _items;
}
set
{
SetProperty(ref _items, value);
}
}
}
public class ChildViewModel
{
public ChildViewModel
{
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
SetProperty(ref _isSelected, value);
}
}
}
How to get selected view model? Maybe use a converter?
<someUserControl DataContext="{Binding ParentViewModel.Items, Converter={x:Static c:GetSelectedItemConverter.Instance}}" />
In converter I can find selected item. Or this is bad idea?
UPDATE 2:
Ok, I beat this problem with Ed Plunkett help. Final version should be:
public class ParentViewModel
{
public ParentViewModel
{
Items = GetItems();
foreach (var item in Items)
{
item.PropertyChanged += ChildViewModel_PropertyChanged;
}
}
private ObservableCollection<ChildViewModel> _items;
public ObservableCollection<ChildViewModel> Items
{
get
{
return _items;
}
set
{
SetProperty(ref _items, value);
}
}
private ChildViewModel _selectedChild;
public ChildViewModel SelectedChild
{
get { return _selectedChild; }
set
{
SetProperty(ref _selectedChild, value);
}
}
private void ChildViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var child = (ChildViewModel)sender;
if (e.PropertyName == nameof(ChildViewModel.IsSelected) && child.IsSelected)
{
SelectedChild = child;
}
}
}
SetProperty()
does that. He's good. – 15ee8f99-57ff-4f92-890c-b56153