부모와 자식의 두 창이 있습니다. 상위 창에는 목록 상자가 있습니다.MVVM에서 ICommand를 사용하여 데이터 변경
MAINVIEW : 나는 매개 변수를 사용하여 ICommand의를 사용하여 선택된 사람 양방향 업데이트를 변경하기 위해 노력하고있어
<Button x:Name="btnUpdate" Content="Update"
Command="{Binding Path=MyCommand}" CommandParameter="{Binding ElementName=lstPerson, Path=SelectedItem}" />
<ListBox x:Name="lstPerson" ItemsSource="{Binding Persons}" />
.
PersonViewModel는 :
private ICommand myCommand;
public ICommand MyCommand
{
get
{
if (myCommand == null)
{
myCommand = new RelayCommand<object>(CommandExecute, CanCommandExecute);
}
return myCommand;
}
}
private void CommandExecute(object parameter)
{
var ew = new EditWindow()
{
DataContext =
new EditViewModel()
{
Name = ((Person) parameter).Name,
Address = ((Person) parameter).Address
}
};
ew.Show();
}
그러나 사람의 선택된 인스턴스 목록 상자에서 변경되지 않습니다. xaml 또는 PersonViewModel에 쓰기 작업을하려면 무엇이 필요합니까?
P.
다음은 명령에 대한 exceution 명령의 매개 변수가 잘못 내 사람
public class Person : INotifyPropertyChanged
{
private string name;
private string address;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged("Name");
}
}
public string Address
{
get
{
return address;
}
set
{
address = value;
OnPropertyChanged("Address");
}
}
}
나는 그것을한다! MainWindow.xaml에서 1. 추가 : : 2 PersonViewModel에서에서 INotifyPropertyChanged를 구현 3. 추가 한 <리스트 박스 X 이름 = "lstPerson는"ItemsSource = selectedItem가 =이/{SelectedPerson 바인딩} "{사람 바인딩}">을 속성 SelectedPerson. 감사합니다. –
니스, 작동합니까? – Marc