【问题标题】:How to use bindable property by Binding in xamarin forms?如何通过在 xamarin 表单中绑定来使用可绑定属性?
【发布时间】:2019-02-10 18:00:22
【问题描述】:

我正在尝试创建一个包含具有特殊设计的选择器的内容视图,并编写了所有可绑定属性,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ShobeekClientApp.Custom.Views.Picker">
    <ContentView.Content>
        <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Picker x:Name="FSPicker" Title="{Binding PickerTitle,Source={x:Reference this}" 
                    ItemsSource="{Binding PickerItemsSource,Source={x:Reference this}}" 
                    ItemDisplayBinding="{Binding PickerItemDisplayBinding,Source={x:Reference this}}"
                    HorizontalOptions="FillAndExpand"/>

        </Grid>
    </ContentView.Content>
</ContentView>

contentView背后的代码:

using System;
using System.Collections;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ShobeekClientApp.Custom.Views
{
    ///for more information  follow this tutorial
    ///https://mindofai.github.io/Creating-Custom-Controls-with-Bindable-Properties-in-Xamarin.Forms/
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Picker : ContentView
    {
        #region selected index property
        public static readonly BindableProperty PickerSelectedIndexProperty = BindableProperty.Create(
           nameof(PickerSelectedIndex), typeof(int), typeof(Picker), -1, BindingMode.TwoWay, propertyChanged: selctedIndexChanged);

        private static void selctedIndexChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (Picker)bindable;
            control.FSPicker.SelectedIndex = (int)newValue;
        }

        public int PickerSelectedIndex
        {
            get { return (int)GetValue(PickerSelectedIndexProperty); }
            set { SetValue(PickerSelectedIndexProperty, value); }
        }
        #endregion

        #region title Property
        public static readonly BindableProperty PickerTitleProperty = BindableProperty.Create(
      nameof(PickerTitle), typeof(string), typeof(Picker), defaultValue: "", defaultBindingMode : BindingMode.TwoWay,
      propertyChanged: titleChanged);

        private static void titleChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (Picker)bindable;
            control.FSPicker.Title = newValue.ToString();
        }

        public string PickerTitle
        {
            get { return (string)GetValue(PickerTitleProperty); }
            set { SetValue(PickerTitleProperty, value); }
        }
        #endregion

        #region items source property
        public static readonly BindableProperty PickerItemsSourceProperty = BindableProperty.Create(
            nameof(PickerItemsSource), typeof(IList), typeof(Picker), null, BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);

        private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (Picker)bindable;
            control.FSPicker.ItemsSource = (IList)newValue;
        }

        public IList PickerItemsSource
        {
            get { return (IList)GetValue(PickerItemsSourceProperty); }
            set { SetValue(PickerItemsSourceProperty, value); }
        }
        #endregion

        public static readonly BindableProperty PickerItemDisplayBindingProperty = BindableProperty.Create(nameof(PickerItemDisplayBinding), typeof(BindingBase), typeof(Picker));
        public BindingBase PickerItemDisplayBinding
        {
            get { return (BindingBase)GetValue(PickerItemDisplayBindingProperty); }
            set { SetValue(PickerItemDisplayBindingProperty, value); }
        }

        public Picker ()
        {
            try
            {
                InitializeComponent();
                BindingContext = this;
                //FSPicker.SetBinding(FSPicker.ItemsSource, new Binding(nameof(Property), source: BindingContext));
                //SetBinding(PickerSelectedIndexProperty, );
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
        }
    }
}

使用控制码:

<customViwes:Picker PickerTitle="{Binding PickerTitle,Mode=TwoWay}"  `PickerItemsSource="{Binding pickerData,Mode=TwoWay}" PickerItemDisplayBinding="{Binding .}"/>`

我想使用 {Binding} 绑定另一个使用此控件的设计中的此属性

如果我使用绝对值,它会成功显示,我将使用绑定,因为我使用 MVVM 结构,它不起作用

【问题讨论】:

  • 请不要将代码发布为图片。不能复制/粘贴,搜索引擎看不到,阅读难度很大
  • 添加了代码而不是图片
  • 您找到解决方案了吗?

标签: c# xaml xamarin.forms controls


【解决方案1】:

您需要为您的根定义一个名称,然后引用根的绑定。

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Name="Root" 
             x:Class="ShobeekClientApp.Custom.Views.Picker">
    <ContentView.Content>
        <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Picker x:Name="FSPicker" Title="{Binding Source={ x:Reference Root }, Path=PickerTitle}" 
                    ItemsSource="{Binding Source={ x:Reference Root }, Path=PickerItemsSource}" 
                    ItemDisplayBinding="{Binding Source={ x:Reference Root }, Path=PickerItemDisplayBinding}" 
                    HorizontalOptions="FillAndExpand"/>

        </Grid>
    </ContentView.Content>
</ContentView>

【讨论】:

  • 添加时不起作用,项目源和标题没有出现任何东西
猜你喜欢
  • 2021-10-02
  • 1970-01-01
  • 2018-02-07
  • 2021-12-23
  • 2016-09-08
  • 2019-01-07
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
相关资源
最近更新 更多