ItemsSource可绑定属性

3

我正在尝试创建一个带有两个项目(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


你使用了 INotifyPropertyChange 吗? - Alan Jones Rios
@AlanJonesRios,是的,它可以工作,当我使用我的Picker而没有内容视图时,但是当我尝试将“ItemsSource”作为“BindableProperty”发送时,它就不行了。 - Nikita
1
很难确定没有完整的代码,但从您展示的内容来看,可能是因为可绑定属性使用了错误的类型?声明的属性使用了DetailedPicker,但在xaml中您正在使用ExtendedPicker。 - App Pack
1
尝试将列表类型更改为“ObservableCollection”。我不知道这是否有帮助。 - micael cunha
1个回答

1

我不确定你能够使用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>

我希望它有所帮助。


需要帮助,我已经做了和你一样的事情,但是当我传递PickerItemsSource="{Binding Path=FacilityVM.Locations}" PickerItemDisplayBinding="{Binding Name}"时,在Name中会出现名称未找到的错误,请问你能帮我吗? - Jasmin Sojitra
当然可以,@JasminSojitra,你能否分享你的问题链接并提供一个MCVE - Diego Rafael Souza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接