Hello,
but I was not clear on how to do this or if using this approach would violate the MVVM design pattern.
No, actually, using INotfiyPropertyChanged
is the official recommended method, please refer to Data binding and MVVM for more details.
You could refer to the following code to modify your ViewModel
so that your ListView
updates SelectedUdf.NAME
as expected:
// in Model Code
public class UserDefinedFields: ObservableObject
{
private string name;
public string NAME
{
get => name;
set => SetProperty(ref name, value);
}
private int id;
public int ID
{
get => id;
set => SetProperty(ref id, value);
}
}
// in ViewModel
public partial class TestViewModel : INotifyPropertyChanged
{
private ObservableCollection<UserDefinedFields> _userdefinedfields;
public ObservableCollection<UserDefinedFields> Userdefinedfields
{
get { return _userdefinedfields; }
set
{
if (_userdefinedfields != value)
{
_userdefinedfields = value;
OnPropertyChanged(); // reports this property
}
}
}
private UserDefinedFields _selectedUdf;
public UserDefinedFields SelectedUdf
{
get => _selectedUdf;
set
{
if (_selectedUdf != value)
{
_selectedUdf = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.