【问题标题】:ItemsSource bindable propertyItemsSource 可绑定属性
【发布时间】: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”。我不知道这是否有帮助

标签: xamarin binding xamarin.forms itemssource


【解决方案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吗?
猜你喜欢
  • 2012-01-08
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 2017-09-18
  • 1970-01-01
  • 2011-11-10
相关资源
最近更新 更多