【问题标题】:How to bind values between two bindable properties in composite control Xamarin Forms?如何在复合控件 Xamarin Forms 中的两个可绑定属性之间绑定值?
【发布时间】:2015-11-25 09:10:19
【问题描述】:

我创建了一个从 Frame 扩展的 Forms 控件,左侧有一个编辑器和一个框视图。我公开了 Frame 的 Text 属性,以便我可以在 XAML 绑定中使用。如果我从 xaml 绑定文本的值,它会出现在编辑器中。但是如何在不触发属性更改的情况下将用户编辑文本设置回框架文本属性?

    public class MyEditor : Frame
    {
    public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(MyEditor), String.Empty);        
    public string Text
    {
        get { return (string)this.GetValue(TextProperty); }
        set
        {
            this.SetValue(TextProperty, value);

            // this set is not calling when used from XAML Bindings 
            if (this.editor != null)
                this.editor.Text = value;
        }
    }

    private Editor editor;
    private BoxView leftView;
    private StackLayout contentHolder;

    public MyEditor()
    {
        this.HasShadow = false;
        this.Padding = 0;
        this.IsClippedToBounds = true;

        contentHolder = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Orientation = StackOrientation.Horizontal,
            Spacing = 0
        };
        this.Content = contentHolder;

        editor = new Editor();
        editor.TextChanged += editor_TextChanged;
        editor.HorizontalOptions = LayoutOptions.FillAndExpand;
        editor.VerticalOptions = LayoutOptions.FillAndExpand;

        leftView = new BoxView()
        {
            IsVisible = false,
            WidthRequest = 5,
            BackgroundColor = Color.FromHex("ff9900")
        };

        contentHolder.Children.Add(leftView);
        contentHolder.Children.Add(editor);
    }

    void editor_TextChanged(object sender, TextChangedEventArgs e)
    {
        // how to update user edited text back to (Text)TextProperty without triggering OnPropertyChanged ? 

        //Text = editor.text; 
        // this triggers the Property change again.
    }

    protected override void OnPropertyChanged(string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);

        //update the Text property of MyEditor to actual editor
        if (propertyName == TextProperty.PropertyName)
        {
            editor.Text = Text;
        }
    }
}

Xaml 代码:

<CustomControl:MyEditor x:Name="cEditor" Text="{Binding Text}"  WidthRequest="300" HeightRequest="150"/>

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    在您的代码中,您在 bindableproperty 的 setter 和 OnPropertyChanged 函数中设置编辑器的文本,并在 OnTextChanged 事件中设置框架的文本 - 3 段代码。

    所有这些都尝试使用双向绑定来绑定这两个文本,如下所示:

    public MyEditor()
    {
    ...
       editor.SetBinding(Editor.TextPropety, new Binding("Text", BindingMode.TwoWay,source:this));
    
    }
    

    同样在您的 Xaml 代码中将绑定模式更改为双向绑定,以反映绑定上下文中的文本更改。实际上,我认为如果您更改 Xaml,它将解决您当前的问题(“将用户编辑文本设置回 Frame Text 属性”),但恕我直言,绑定这两个文本更优雅。

    【讨论】:

    • 是的,你是对的,我们需要绑定两个 TextProperties,还需要在 Xaml 中设置像这样的 TwoWay 绑定 Text="{Binding Text, Mode=TwoWay}" 否则在组件级别,我们需要以两种方式创建 Bindable 属性 public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(MyEditor), String.Empty, BindingMode.TwoWay); 这很好用……
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2021-05-20
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多