[英]Picker with Data Using Data Binding XAMARIN
我有2個字段的模型Names
。
public class Names
{
public string ID { get; set; }
public string Name { get; set; }
}
我需要從模型Names
獲取所有名稱,然后在XAMARIN中進行選擇。
<Picker Title="Select a name"
ItemsSource="{Binding AllNames}"
ItemDisplayBinding="{Binding Name}" />
最簡單的方法是什么?
您可能想要創建一個ObservableCollection,其中包含要在視圖模型內的列表內使用的對象。 像這樣:
public class ViewModel
{
ObservableCollection<Names> allNames = new ObservableCollection<GroupedReportModel>();
public ObservableCollection<Names> AllNames
{
get { return allNames; }
set { SetProperty(ref allNames, value); }
}
}
SetProperty是通過將INotifyPropertyChanged實現添加到viewModel上而獲得的替代。
我為此使用的代碼如下所示:
protected bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName]string propertyName = "", Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.