繁体   English   中英

在包含 Picker 的自定义控件中绑定 ItemSource 属性

[英]Binding ItemSource property in a custom control that contains a Picker

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

注意:我尝试了几乎所有在“Stack OverFlow”或“forums.xamarin”上找到的解决方案,但没有一个对我有用。

这是我的代码:

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

   <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 方法从未被调用,并且几乎所有与我类似的问题都得到了类似的回答,这表明将此方法放在控件类中。

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

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

这是上面 XAML 的 ViewModel 中的猴子列表声明:

 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 永远不会被调用,因为知道 Binding 上下文是 ViewModel

当您在 CustomControl 中设置 bindingcontext 时,例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM