ItemsSource 可绑定 属性

ItemsSource bindable property

我正在尝试使用 2 个项目(LabelDatePicker)制作 ContentView,我需要为第二个项目发送 ItemsSource 作为可绑定 属性.

我尝试使用 BindingBase,但没有成功。

Xaml:

<Grid>
    <Label
        Text="Text"
        TextColor="Black"
        VerticalOptions="CenterAndExpand" />

    <controls:ExtendedPicker
        Title="Title"
        HorizontalOptions="End"
        ItemDisplayBinding="{Binding PickerItemDisplayBinding, Source={x:Reference This}}"
        ItemsSource="{Binding PickerItemsSource, Source={x:Reference This}}"
        SelectedIndex="{Binding PickerSelectedIndex, Source={x:Reference This}}" />
</Grid>

Xaml.cs:

public static readonly BindableProperty PickerItemsSourceProperty = BindableProperty.Create(
    "PickerItemsSource",
    typeof(IList),
    typeof(DetailedPicker));

public static readonly BindableProperty PickerSelectedIndexProperty = BindableProperty.Create(
    "PickerSelectedIndex",
    typeof(int),
    typeof(DetailedPicker));

public static readonly BindableProperty PickerItemDisplayBindingProperty = BindableProperty.Create(
    "PickerItemDisplayBinding",
    typeof(BindingBase),
    typeof(DetailedPicker));


public IList PickerItemsSource
{
    get => (IList) GetValue(PickerItemsSourceProperty);
    set => SetValue(PickerItemsSourceProperty, value);
}

public int PickerSelectedIndex
{
    get => (int) GetValue(PickerSelectedIndexProperty);
    set => SetValue(PickerSelectedIndexProperty, value);
}

public BindingBase PickerItemDisplayBinding
{
    get => (BindingBase) GetValue(PickerItemDisplayBindingProperty);
    set => SetValue(PickerItemDisplayBindingProperty, value);
}

如何将 ItemsSource 绑定为 BindableProperty 以用于 ContentView

我不确定您是否可以将 x:Reference 用于 this 关键字。我从来没有听说过这样的事情。

虽然我想您可以通过为您的 ContentView 提供 x:Name 来解决这个问题。就像这样:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:Your.Controls.Namespace;assembly=packageName"
             x:Class="Your.Another.Control.Namespace.DetailedPicker"
             x:Name="MyThisReference">
    <ContentView.Content>
        <Grid>
            <!-- Your label and picker goes here -->
            <!-- ... -->
                         ItemsSource="{Binding PickerItemsSource, Source={x:Reference MyThisReference}}"
            <!-- ... -->
        </Grid>
    </ContentView.Content>
</ContentView>

希望对您有所帮助。