Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单中的Picker SelectedItem绑定问题_C#_Data Binding_Xamarin.forms_Binding_Picker - Fatal编程技术网

C# Xamarin表单中的Picker SelectedItem绑定问题

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}"> <

我有一个绑定到可观察收集对象的选择器。这些项是从web服务(json数据)填充的

选择器正确显示数据,但SelectedItem不起作用

以下是我的xaml代码:

  <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>