【发布时间】:2018-08-27 21:12:51
【问题描述】:
我正在尝试使用 2 个项目(Label 和 DatePicker)制作 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 吗?
-
@AlanJonesRios,是的,当我使用没有内容视图的 Picker 时,它可以工作,但是当我尝试将
ItemsSource发送为BindableProperty时,它不是。 -
如果没有完整的代码很难分辨,但从您所展示的内容来看,可能是因为可绑定属性的类型错误?声明的属性使用DetailedPicker,但在xaml中您使用的是ExtendedPicker
-
尝试将列表类型更改为“ObservableCollection”。我不知道这是否有帮助
标签: xamarin binding xamarin.forms itemssource