在包含选择器的自定义控件中绑定 ItemSource 属性

Binding ItemSource property in a custom control that contains a Picker

我正在尝试创建一个包含带有 Xamarin.Forms 的选择器的自定义控件。 问题是当尝试绑定 ItemSource 属性 时,它永远不会被绑定,当我触摸移动设备上的自定义控件时,它显示一个没有绑定项目的空对话框。

注意:我几乎尝试了在“Stack OverFlow”或“forums.xamarin”上找到的所有解决方案,其中none对我有用.

这是我的代码:

对于自定义控件 XAML 文件 - 以“HitPicker”命名 - :

   <Picker x:Name="PickerField" 
        HeightRequest="46" 
        TitleColor="{Binding TitleColor}"
        TextColor="{Binding TextColor}" 
        BackgroundColor="{Binding BackgroundColor}"
        Unfocused="Handle_Unfocused" 
        Focused="Handle_Focused"
        SelectedItem="{Binding SelectedItem}"
        ItemsSource="{Binding ItemsSource}">
   </Picker>

对于自定义控件 cs 文件:

public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(List<string>), typeof(HitPicker), default(List<string>), BindingMode.TwoWay, null, OnItemsSourceChanged);

public List<string> ItemsSource
    {
        get => (List<string>)GetValue(ItemsSourceProperty);
        set => SetValue(ItemsSourceProperty, value);
    }

public HitPicker()
{
    InitializeComponent();
    BindingContext = this;
}

private static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        var picker = (bindable as HitPicker).PickerField;
        picker.Items.Clear();
        var newList = newvalue as List<string>;
        if (newvalue != null)
        {
            foreach (var item in newList)
            {
                picker.Items.Add(item.ToString());
            }
        }
    }

知道永远不会调用 OnItemsSourceChanged 方法,并且几乎每个与我类似的问题都有类似的答案,这表明将此方法放在控件中 class。

对于使用此控件的 XAML 文件:

<controls:HitPicker ItemsSource="{Binding MonkeyList}" Title="Select monky" BackgroundColor="Azure"></controls:HitPicker>

这是 ViewModel 中针对上述 XAML:

的猴子列表声明
 private List<string> _lst = new List<string>{
            "Baboon",
            "Capuchin Monkey",
            "Blue Monkey",
            "Squirrel Monkey",
            "Golden Lion Tamarin",
            "Howler Monkey",
            "Japanese Macaque"
        };

    public List<string> MonkeyList
    {
        get => _lst;
        set
        {
            _lst = value;
            OnPropertyChanged();
        }
    }

MonkeyList getter 也从未被调用,知道绑定上下文是 ViewModel

当您像这样在 CustomControl 中设置绑定上下文时

public HitPicker()
{
    InitializeComponent();
    BindingContext = this;
}

它将打破自定义控件和 ContentPage 之间的绑定。

所以你可以像下面这样修改代码

在HitPicker.xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            //...
             x:Name="pickerView" //set name of page >
   <Picker x:Name="PickerField" 
        HeightRequest="46" 
        TitleColor="{Binding Source={x:Reference pickerView}, Path=TitleColor}"
        TextColor="{Binding Source={x:Reference pickerView}, Path=TextColor}" 
        BackgroundColor="{Binding Source={x:Reference pickerView}, Path=BackgroundColor}"
        Unfocused="Handle_Unfocused" 
        Focused="Handle_Focused"
        SelectedItem="{Binding Source={x:Reference pickerView}, Path=SelectedItem}">
   </Picker>

在HitPicker.xaml.cs

public HitPicker()
{
    InitializeComponent();
    //BindingContext = this;
}