ItemsSource 可绑定属性

Posted

技术标签:

【中文标题】ItemsSource 可绑定属性【英文标题】:ItemsSource bindable property 【发布时间】:2018-08-27 21:12:51 【问题描述】:

我正在尝试使用 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 绑定为BindablePropertyContentView

【问题讨论】:

你使用了 INotifyPropertyChange 吗? @AlanJonesRios,是的,当我使用没有内容视图的 Picker 时,它可以工作,但是当我尝试将 ItemsSource 发送为 BindableProperty 时,它不是。 如果没有完整的代码很难分辨,但从您所展示的内容来看,可能是因为可绑定属性的类型错误?声明的属性使用DetailedPicker,但在xaml中您使用的是ExtendedPicker 尝试将列表类型更改为“ObservableCollection”。我不知道这是否有帮助 【参考方案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" 在名称中给出错误名称不是发现错误,你能帮帮我吗 好的,@JasminSojitra 你能把你问题的链接分享给MCVE吗?

以上是关于ItemsSource 可绑定属性的主要内容,如果未能解决你的问题,请参考以下文章

c#wpf combobox将source绑定到一个collection,item作为另一个collection的属性

更改 Button.Click 上的 ListBox.ItemsSource 绑定属性?

如何将 DataGridTemplateColumn.Visibility 绑定到 DataGrid.ItemsSource 之外的属性?

如何将 ItemsControl.ItemsSource 与 XAML 中的属性绑定?

急等,在wpf中怎么才能把image控件的source属性绑定到listview中的选中项

WPF中ListBox的绑定