Xamarin Forms binding within ContentPresenter

Tobias

I created a custom control with a ControlTemplate. In the control template I use two ContentPresenters.

One binds to the Content-property of the base class (ContentView) the other one binds to a OtherContent-property of type View.

MyControl.xaml.cs

public static readonly BindableProperty ReadonlyContentProperty = BindableProperty.Create("ReadonlyContent", typeof(View), typeof(ActivatableContent), propertyChanged: OnReadonlyContentChanged);

MyControl.xaml

<ContentView x:Class="MyControl">
   <ContentView.ControlTemplate>
      <...some other stuff>
      <ContentPresenter x:Name="CPOther" Content="{TemplateBinding OtherContent}" />
      <ContentPresenter x:Name="CPBase" Content="{TemplateBinding Content}" />
   </ContentView.ControlTemplate>
</ContentView>

Now when using the Control only the binding within the ContentPresenter binding to base classes content (CPBase) works. The binding within the Content of CPOther is not working.

<MyControl>
    <MyControl.OtherContent>
        <!-- Binding not working -->
        <Label Text="{Binding SomeBinding}" />
    </MyControl.OtherContent>
    <!-- Binding working -->
    <Label Text="{Binding SomeBinding}" />
</MyControl> 

I guess it is not working because the BindingContext gets lost for CPOther. Is that correct? Can anyone tell me how to "fix" this problem? Or is it even a bug in XF?


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

namespace Com.Tanken.Xamarin.Forms.Controls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    [ContentProperty("Content")]
    public partial class ActivatableContent : ContentView
    {
        public static readonly BindableProperty IsReadonlyProperty = BindableProperty.Create("IsReadonly", typeof(bool), typeof(ActivatableContent), true);

        public static readonly BindableProperty ReadonlyContentProperty = BindableProperty.Create("ReadonlyContent", typeof(View), typeof(ActivatableContent));

        public bool IsReadonly
        {
            get { return (bool)GetValue(IsReadonlyProperty); }
            set { SetValue(IsReadonlyProperty, value); }
        }

        public View ReadonlyContent
        {
            get
            {
                return (View)GetValue(ReadonlyContentProperty);
            }
            set { SetValue(ReadonlyContentProperty, value); }
        }

        public ActivatableContent()
        {
            InitializeComponent();
        }

        private void OnToggleIsReadonly(object sender, EventArgs e)
        {
            IsReadonly = !IsReadonly;
        }
    }
}
Tobias

Seems like I was write with the assumption that it was because of missing or unset BindingContext. I could "solve" it by adding the following code to the control:

protected override void OnBindingContextChanged()
{
    base.OnBindingContextChanged();
    var controlTemplate = ControlTemplate;
    if (ReadonlyContent != null && controlTemplate != null)
    {
        SetInheritedBindingContext(ReadonlyContent, BindingContext);
    }
    if (WritableContent != null && controlTemplate != null)
    {
        SetInheritedBindingContext(WritableContent, BindingContext);
    }
}

Now another problem occured with multiple ContentPresenters in one ControlTemplate, but I worked around it with just using one ContentPresenter and change its content.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related