UserControl
ViewModel
공유가 여러 개 있습니다.WPF TwoWay 바인딩에 여러 UserControl
그것이 행의 사용자 클릭은 로우의 내용을 볼 수있어 DataGrid
(실제 구조가 더 복잡하다). 나는 그리드에서 SelectionChanged
을 처리 할 때
문제
는, 나는 ContactDetail를 업데이트 할 수있는 공유ViewModel
를 업데이트하지만
TextBoxes
의 값 (객체가 ContactDetail에 업데이트되지만 값이 표시되지 않음) 업데이트하지 않습니다.
ListContact.xaml.cs
public void contactsTable_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
contacts.current_identity = //Get the associated `IdentityViewModel`
}
ContactDetail.xaml.cs
public partial class ContactDetail : UserControl
{
public ContactsViewModel contacts;
public DetailContact(ContactsViewModel contacts)
{
InitializeComponent();
this.contacts = contacts;
this.DataContext = contacts;
}
}
<UserControl x:Class="ContactDetail">
<TextBox Name='address' Text="{Binding Path=contacts.current_identity.address, Mode=TwoWay}"/>
<TextBox Name='phone' Text="{Binding Path=contacts.current_identity.phone, Mode=TwoWay}"/>
<TextBox Name='email' Text="{Binding Path=contacts.current_identity.email, Mode=TwoWay}"/>
</UserControl>
ContactDetail.xaml ContactsViewModel.cs (IdentityViewModel이 같은 구조를 사용)
public class ContactsViewModel : INotifyPropertyChanged
{
private List<Contact> _contacts;
public List<Contact> contacts;
{
get { return _contacts; }
set { _contacts = value; OnPropertyChanged("contacts"); }
}
private IdentityViewModel _current_identity;
public IdentityViewModel current_identity
{
get { return _current_identity; }
set { _current_identity = value; OnPropertyChanged("current_identity"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
문제는 왜이 작품과 어떻게 새 값을 표시하도록 ContactDetail
을 통보 않습니다입니까?
참조 ("포인터") 자체를 변경하지 않는 한 참조 형식의 인스턴스를 'ref'로 전달할 필요가 없습니다. 'class' 인스턴스는 항상 참조에 의해 전달 될 것이고'struct'가 복사 될 것입니다. – dymanoid
뷰 모델이 필요할 때 'PropertyChanged' 이벤트를 발생 시키시겠습니까? 보기 - 모델을 게시하십시오. – dymanoid
@dymanoid 편집되었습니다. 그렇습니다. – BadMiscuit