You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

制作含Label与DatePicker的ContentView:ItemsSource绑定属性问题

解决ContentView中ExtendedPicker可绑定属性的问题

我来帮你搞定这个把ExtendedPicker的ItemsSource做成可绑定属性的问题!从你给出的代码片段来看,大概率是可绑定属性的定义不规范,或者XAML里的绑定引用出了小问题,咱一步步来修复:

第一步:正确定义ContentView的可绑定属性

首先在你的ContentView后台代码(比如YourContentView.xaml.cs)里,按照Xamarin.Forms的规范定义两个可绑定属性:PickerItemsSource和PickerItemDisplayBinding,这样外部才能通过Binding给它们传值。

public partial class YourContentView : ContentView
{
    // 定义PickerItemsSource可绑定属性
    public static readonly BindableProperty PickerItemsSourceProperty =
        BindableProperty.Create(nameof(PickerItemsSource), typeof(IEnumerable), typeof(YourContentView), default(IEnumerable));

    public IEnumerable PickerItemsSource
    {
        get => (IEnumerable)GetValue(PickerItemsSourceProperty);
        set => SetValue(PickerItemsSourceProperty, value);
    }

    // 定义PickerItemDisplayBinding可绑定属性
    public static readonly BindableProperty PickerItemDisplayBindingProperty =
        BindableProperty.Create(nameof(PickerItemDisplayBinding), typeof(BindingBase), typeof(YourContentView), default(BindingBase));

    public BindingBase PickerItemDisplayBinding
    {
        get => (BindingBase)GetValue(PickerItemDisplayBindingProperty);
        set => SetValue(PickerItemDisplayBindingProperty, value);
    }

    public YourContentView()
    {
        InitializeComponent();
    }
}

第二步:修正XAML中的绑定引用

你提供的XAML里Source={x:Reference Thi...明显是输入不完整,应该是x:Reference This(前提是你给ContentView设置了x:Name="This")。然后把ExtendedPicker的ItemsSource和ItemDisplayBinding绑定到ContentView的可绑定属性上:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:YourNamespace.Controls"
             x:Class="YourNamespace.YourContentView"
             x:Name="This"> <!-- 一定要设置这个x:Name,绑定才能引用到 -->
    <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}}" />
    </Grid>
</ContentView>

第三步:在父页面中使用这个ContentView

现在你可以在父页面的XAML里,像使用普通控件一样给这两个可绑定属性传值了:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:YourNamespace"
             x:Class="YourNamespace.MainPage">
    <StackLayout>
        <local:YourContentView
            PickerItemsSource="{Binding YourItemList}"
            PickerItemDisplayBinding="{Binding DisplayText}" />
    </StackLayout>
</ContentPage>

这里要注意:

  • 确保父页面的ViewModel里有YourItemList(一个IEnumerable类型的集合)和对应的实体类有DisplayText属性(或者你要显示的字段名)。
  • 如果你的ExtendedPicker有自定义逻辑,要确保它本身的ItemsSource和ItemDisplayBinding是可以正常绑定的,没有内部逻辑覆盖这些属性。

内容的提问来源于stack exchange,提问作者Nikita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:13:55