Почему родительский ViewModel в дочернем становится null ?
Для взаимодействия между дочерней и родительской ViewModel, я передал ссылку на родительскую ViewModel в дочернюю ViewModel .
Когда вовремя отладки я открываю окно ManagerIndexesViewModel.IndexesViewModel, то член ManagerIndexesViewModel, который находится в IndexesViewModel не равен нулю.
Когда я вбираю строку в IndexesView.DataGrid, то в свойстве SelectedIndexCalculation я получаю this.managerIndexesViewModel = null (в строке this.managerIndexesViewModel.SelectedIndexCalculation = SelectedIndexCalculation;).
IndexesViewModel
public IndexCalculation SelectedIndexCalculation
{
get { return selectedIndexCalculation; }
set
{
selectedIndexCalculation = value;
Debug.WriteLine($"--- --- --- --- --- --- --- --- ---");
Debug.WriteLine($"IndexesViewModel--selectedIndexCalculation -- {selectedIndexCalculation.Name}");
this.managerIndexesViewModel.SelectedIndexCalculation = SelectedIndexCalculation;
RaisePropertyChanged(nameof(SelectedIndexCalculation));
}
}
Проект: https://github.com/jhon65496/WpfFrmApp1QPublic
Минимальный код
ManagerIndexesViewModel
public class ManagerIndexesViewModel : BaseVM
{
IndexesViewModel indexesViewModel;
//. Code ....
public ManagerIndexesViewModel()
{
// ViewModel
this.indexesViewModel = new IndexesViewModel(this);
// Code
// .....
// View
IndexesView = this.indexesViewModel;
// Code
// .....
SelectedIndexCalculation = indexesViewModel.SelectedIndexCalculation;
}
IndexesViewModel
public class IndexesViewModel : BaseVM
{
public ManagerIndexesViewModel managerIndexesViewModel;
public IndexesViewModel(ManagerIndexesViewModel managerIndexesViewModel)
{
this.managerIndexesViewModel = managerIndexesViewModel;
LoadDataTest();
}
public IndexesViewModel()
{
LoadDataTest();
}
private ObservableCollection<IndexCalculation> calculationIndexs;
public ObservableCollection<IndexCalculation> СalculationIndexs
{
get { return calculationIndexs; }
set
{
calculationIndexs = value;
RaisePropertyChanged(nameof(СalculationIndexs));
}
}
private IndexCalculation selectedIndexCalculation;
public IndexCalculation SelectedIndexCalculation
{
get { return selectedIndexCalculation; }
set
{
selectedIndexCalculation = value;
Debug.WriteLine($"--- --- --- --- --- --- --- --- ---");
Debug.WriteLine($"IndexesViewModel--selectedIndexCalculation -- {selectedIndexCalculation.Name}");
RaisePropertyChanged(nameof(SelectedIndexCalculation));
}
}
[
3



<vvm:IndexesViewModel/>- это тоже самое, если вы сделаетеDataContext = new IndexesViewModel(), и вы будете постоянно при рендере данной страницы инициализировать новый контекст, и самое важное, будет инициализироваться класс с конструктором без параметров, ну а теперь найдите в своем кодеpublic IndexesViewModel() { .. }и посмотрите, задаете вы тамmanagerIndexesViewModelили нет. Вообще установка контекста в XAML плохо. – EvgeniyZ Oct 18 '23 at 18:12public IndexesViewModel()и узнаете, почему. – aepot Oct 18 '23 at 18:43aepot– eusataf Oct 18 '23 at 21:28App, аprotected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); ...здесь... }– aepot Oct 18 '23 at 22:39