【发布时间】:2018-03-13 13:03:27
【问题描述】:
我有一个 xaml 代码:
<Picker ItemsSource="{Binding profiles}" ItemDisplayBinding="{Binding name}" SelectedItem="{Binding selectedProfile}" HorizontalOptions="FillAndExpand" Margin="0, 0, 20, 0" VerticalOptions="Center"/>
在我定义的cs文件中:
BindingContext = this;
和
private ObservableCollection<Profile> _profiles = new ObservableCollection<Profile>();
public ObservableCollection<Profile> profiles
{
get { return _profiles; }
set { _profiles = value; }
}
配置文件类是:
public class Profile
{
private string _name = "New profile";
public string name
{
get { return _name; }
set {
_name = value;
}
}
}
当我添加/删除元素并在下拉列表或代码中选择新元素(selectedProfile = profiles[index])时,它可以正常工作。 但是当我尝试重命名配置文件时会出现问题。我更改了个人资料名称,但 Picker 没有更新它,我看到了旧值。
我也试过这个。但是现在的结果。
public class Profile : INotifyPropertyChanged
{
private string _name = "New profile";
public string name
{
get { return _name; }
set {
_name = value;
OnPropertyChanged("name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
【问题讨论】:
-
为您的 Profile 类实现 INotifyPropertyChanged 接口
-
杰拉德,我更新了帖子,没用
标签: c# xamarin binding xamarin.forms