ItemsSource可绑定属性

时间:2018-03-19 10:33:19

标签: xamarin binding xamarin.forms itemssource

我尝试使用2个项目ContentViewLabel制作DatePicker,我需要将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

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以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>

我希望它有所帮助。