[英]Xamarin.Forms - Picker selected Item binding not responding
我制作简单MVVM有约束力的picker
中xamarin.forms领域。 我正在遵循本指南xamarin指南设置选择器的绑定
所以我做了一个模型:
public class Operation
{
public int Number { get; set; }
public string Name { get; set; }
}
一个ViewModel:
private List<Operation> _operations;
public List<Operation> Operations
{
get { return _operations; }
set
{
_operations = value;
OnPropertyChanged();
}
}
并查看:
<Picker
ItemsSource="{Binding Operations}"
ItemDisplayBinding="{Binding Number}"
SelectedItem = "{Binding SelectedOperation}"/>
<Entry x:Name="HelpEntry"
Text="{Binding SelectedOperation.Name}" />
在选择器列表中,项目显示正确,但是当我选择项目编号时,不会显示条目内的绑定。
Ouestion是,我做错了什么?
顺便说一句..我这样做是因为我需要通过使用HelpEntry.Text在我的代码隐藏部分中获得一个选定的Operation's Name
作为变量。 这不是最聪明的方式,你有更好的想法吗?
任何帮助都会非常感激。
您的ViewModel还应包含SelectedOperation
属性,该属性还应在其setter中调用OnPropertyChanged
方法。
您还应该考虑在查看模型时使用ObservableCollection
而不是List
。
确保ViewModel实现了INotifyPropertyChanged接口。 轻松完成此操作的方法是创建一个实现接口的BaseViewModel,然后从该基类继承所有具体的视图模型类。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class MainPageVM : ViewModelBase
{...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.