Xamarin表单自定义选取器SelectItem绑定不工作
我已经用xaml创建了一个自定义选择器控件Xamarin表单自定义选取器SelectItem绑定不工作,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我已经用xaml创建了一个自定义选择器控件PCPicker(为将来考虑了不引人注目的验证),如下所示: <Label x:Name="ControlLabel" Style="{DynamicResource InputLabel}" Text="{Binding LabelText}"/> <Picker x:Name="ControlPicker" ItemsSour
PCPicker
(为将来考虑了不引人注目的验证),如下所示:
<Label x:Name="ControlLabel"
Style="{DynamicResource InputLabel}"
Text="{Binding LabelText}"/>
<Picker x:Name="ControlPicker"
ItemsSource="{Binding Src}"
Title="{Binding PlaceHolderText}"
Style="{DynamicResource PCPickerStyle}"
SelectedIndex="{Binding Index,Mode=TwoWay}"
SelectedItem="{Binding SelectedOption,Mode=OneWayToSource}"
/>
用法:
<cc:PCPicker BindingContext="{x:Binding M}"
LabelText="* Location"
PlaceHolderText="Select Cat"
Src="{Binding Cats}"
SelectedOption="{Binding SelectedCat, Mode=TwoWay}"
Index="{Binding Position, Mode=TwoWay}"
DisplayMember="{Binding Name}"/>
通过上面的操作,我可以使Display绑定和ItemSource绑定正常工作。但是,SelectedItem值绑定始终为空。我将SelectedIndex绑定作为测试,但它也始终为0。我已经确保绑定
模式
为双向
,但我仍然总是得到一个空的SelectedItem。非常感谢您的帮助。您是否在ViewModel中实现了INotifyPropertyChanged
ViewModels通常实现INotifyPropertyChanged接口,这意味着该类在其某个属性发生更改时触发PropertyChanged事件。Forms中的数据绑定机制将处理程序附加到此PropertyChanged事件,以便在属性更改时通知它,并使用新值更新目标
因此,您可以改进videmodel,如下所示
public class MyViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
ObservableCollection<Cat> Cats { get; set; }
private Cat selectedCat;
public Cat SelectedCat
{
get
{
return selectedCat;
}
set
{
if (selectedCat != value)
{
selectedCat = value;
NotifyPropertyChanged();
// do something you want
}
}
}
private int position=0;
public int Position
{
get
{
return position;
}
set
{
if (position != value)
{
position = value;
NotifyPropertyChanged();
}
}
}
public MyViewModel()
{
//...
}
}
公共类MyViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
可观察收集猫{get;set;}
私人猫选择猫;
公共猫选择猫
{
得到
{
返回selectedCat;
}
设置
{
如果(selectedCat!=值)
{
selectedCat=值;
NotifyPropertyChanged();
//做你想做的事
}
}
}
私有int位置=0;
公共int位置
{
得到
{
返回位置;
}
设置
{
如果(位置!=值)
{
位置=值;
NotifyPropertyChanged();
}
}
}
公共MyViewModel()
{
//...
}
}
您是否在ViewModel中实现了INotifyPropertyChanged
ViewModels通常实现INotifyPropertyChanged接口,这意味着该类在其某个属性发生更改时触发PropertyChanged事件。Forms中的数据绑定机制将处理程序附加到此PropertyChanged事件,以便在属性更改时通知它,并使用新值更新目标
因此,您可以改进videmodel,如下所示
public class MyViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
ObservableCollection<Cat> Cats { get; set; }
private Cat selectedCat;
public Cat SelectedCat
{
get
{
return selectedCat;
}
set
{
if (selectedCat != value)
{
selectedCat = value;
NotifyPropertyChanged();
// do something you want
}
}
}
private int position=0;
public int Position
{
get
{
return position;
}
set
{
if (position != value)
{
position = value;
NotifyPropertyChanged();
}
}
}
public MyViewModel()
{
//...
}
}
公共类MyViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
可观察收集猫{get;set;}
私人猫选择猫;
公共猫选择猫
{
得到
{
返回selectedCat;
}
设置
{
如果(selectedCat!=值)
{
selectedCat=值;
NotifyPropertyChanged();
//做你想做的事
}
}
}
私有int位置=0;
公共int位置
{
得到
{
返回位置;
}
设置
{
如果(位置!=值)
{
位置=值;
NotifyPropertyChanged();
}
}
}
公共MyViewModel()
{
//...
}
}
在viewmodel中提供代码。在viewmodel中提供代码。我的视图模型实现INotifyPropertyChanged接口。如果我直接绑定到选取器,也可以使用相同的方法,但绑定到自定义选取器中的属性时则不行。您可以提供一个示例,以便我可以在自己的一侧对其进行测试。我的视图模型实现了INotifyPropertyChanged接口。如果我直接绑定到选取器,也可以使用相同的方法,但当绑定到自定义选取器中的属性时就不行了。您可以提供一个示例,这样我就可以在自己的一侧对其进行测试。