C# Xamarin表单中的Picker SelectedItem绑定问题
我有一个绑定到可观察收集对象的选择器。这些项是从web服务(json数据)填充的 选择器正确显示数据,但SelectedItem不起作用 以下是我的xaml代码:C# Xamarin表单中的Picker SelectedItem绑定问题,c#,data-binding,xamarin.forms,binding,picker,C#,Data Binding,Xamarin.forms,Binding,Picker,我有一个绑定到可观察收集对象的选择器。这些项是从web服务(json数据)填充的 选择器正确显示数据,但SelectedItem不起作用 以下是我的xaml代码: <Picker ItemsSource="{Binding RatesTax}" ItemDisplayBinding="{Binding Code}" SelectedItem="{Binding SourceRate, Mode=TwoWay}"> <
<Picker
ItemsSource="{Binding RatesTax}"
ItemDisplayBinding="{Binding Code}"
SelectedItem="{Binding SourceRate, Mode=TwoWay}">
</Picker>
这就是房产
public ObservableCollection<RatesView> RatesTax { get; set; }
以及SourceRate属性
public RatesView SourceRate
{
set
{
if (sourceRate != value)
{
sourceRate = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SourceRate"));
}
}
get
{
return sourceRate;
}
}
我在SourceRate中放置了一个断点,以查看它在拾取选择器的某些数据时是否进入,但它没有进入SourceRate过程。我认为SelectedItem=“{Binding SourceRate}未按预期工作
感谢您的时间!属性
SelectedItem
必须与集合中的项目类型相同。因此,在您的情况下,SourceRate
属性及其支持字段SourceRate
必须为RatesView
类型
另外,通过在绑定中设置Mode=TwoWay
,确保绑定是双向的:
<Picker
ItemsSource="{Binding RatesTax}"
ItemDisplayBinding="{Binding Code}"
SelectedItem="{Binding SourceRate, Mode=TwoWay}">
</Picker>
这可能是Xamarin方面的一个错误。如果您将SourceRate设置为字符串,则在选择时将获得所选代码。由于某些原因,它不会路由绑定对象,而只显示值。这似乎不是期望的/预期的行为。SelectedItem和ItemsSource的类型不匹配谢谢您的回答!因此SourceRate new是否为RateStatx对象?public RatesView SourceRate{set{if(SourceRate!=value){SourceRate=value;PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(“SourceRate”);}获取{return sourceRate;}}
private RatesView sourceRate;
是的,但不要忘记也要创建支持字段sourceRate
键入RatesView
。如果我在public RatesView sourceRate中设置断点,我会进行更改,但仍然不起作用{
当您从选择器no?中选择一个项目时,它将不得不停止,因为它不会弯曲。请尝试更改您的绑定,使其如下所示:SelectedItem=“{binding SourceRate,Mode=TwoWay}”
您的意思是,如果您在SourceRate
setter中设置断点,则在更改所选项目时它不会被击中吗?
public RatesView SourceRate
{
set
{
if (sourceRate != value)
{
sourceRate = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SourceRate"));
}
}
get
{
return sourceRate;
}
}
<Picker
ItemsSource="{Binding RatesTax}"
ItemDisplayBinding="{Binding Code}"
SelectedItem="{Binding SourceRate, Mode=TwoWay}">
</Picker>