【发布时间】:2020-02-28 18:52:18
【问题描述】:
这是 ChildViewModel 的类:
public class ChildViewModel : Screen
{
private string imie = string.Empty;
private string nazwisko = string.Empty;
private string wiek = string.Empty;
private Person person;
private ObservableCollection<Person> personColl;
private MainViewModel mainView = new MainViewModel();
public ChildViewModel(Person person, ObservableCollection<Person> personColl)
{
this.person = person;
this.personColl = personColl;
this.Wyswietl();
}
public string ImieTxt
{
get => this.imie;
set
{
this.imie = value;
this.NotifyOfPropertyChange(() => this.ImieTxt);
}
}
public string NazwiskoTxt
{
get => this.nazwisko;
set
{
this.nazwisko = value;
this.NotifyOfPropertyChange(() => this.NazwiskoTxt);
}
}
public string WiekTxt
{
get => this.wiek;
set
{
this.wiek = value;
this.NotifyOfPropertyChange(() => this.WiekTxt);
}
}
public void Zmien()
{
this.personColl[mainView.DataGridIndex].Imie = this.ImieTxt;
this.personColl[mainView.DataGridIndex].Nazwisko = this.NazwiskoTxt;
this.personColl[mainView.DataGridIndex].Wiek = this.WiekTxt;
this.TryClose();
}
private void Wyswietl()
{
this.ImieTxt = this.person.Imie;
this.NazwiskoTxt = this.person.Nazwisko;
this.WiekTxt = this.person.Wiek;
}
}
单击“Zmien”按钮后,我不知道如何将新数据从 ChildView 上传到 MainView 中的 dataGrid。在 MainView 我有 dataGrid,我从 MainViewModel 加载列表中的数据。单击按钮“Zmien”后,新数据不会加载到 dataGrid 中。
也许你知道怎么做?
【问题讨论】:
-
嗨!也许this 可能会有所帮助。简而言之,集合应该在父集合中,并且包含 DataGrid 行的列表或数据。此集合可以通过这种方式附加到 xaml 中的
DataGrid:<DataGrid ItemsSource="{Binding CollectionName}"/>。 -
您的 xaml 与您的绑定方式在哪里?您的子虚拟机也在创建 MainVM!!!!!!
标签: c# wpf mvvm datagrid caliburn.micro