Forum Xamarin.Forms

Need to set value for base property not for derived class property of created using new in xaml.

saravananMadheswaransaravananMadheswaran Member ✭✭
edited December 2019 in Xamarin.Forms

Hi all,

I am working under design the border like control in Xamarin forms. For that I need add content property to my control but it already available in base class. Using new keyword we can create the same property in derived class.

But the question is how to define and declare the base content in xaml page?

Base class - derived from content view.

 public class BaseBoxView : ContentView
    {

    }

Derived class have new conent proverty which also present in base.
DerivedBorderView.xaml.cs

public partial class DerivedBorderView : BaseBoxView
{
public DerivedBorderView()
{
InitializeComponent();
}

    public static readonly new BindableProperty ContentProperty =
      BindableProperty.Create("Content", typeof(View), typeof(DerivedBorderView), null, BindingMode.Default, null, OnContentPropertyChanged);

    public new View Content
    {
        get { return (View)GetValue(ContentProperty); }
        set { this.SetValue(ContentProperty, value); }
    }

    private static void OnContentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        DerivedBorderView sfButton = bindable as DerivedBorderView;
        sfButton.OnContentPropertyChanged();
    }

    private void OnContentPropertyChanged()
    {
      if(this.Content != base.Content)
        {
            //i need to leave content as null, have to set base content in xaml itselt
        }
    }
}

DerivedBorderView.xaml : I need to set below content to base content property and leave derived property as null for some reason. Is this possible in xaml code?

<?xml version="1.0" encoding="UTF-8"?>
<local:BaseBoxView x:Name="sfbutton" xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
           xmlns:local="clr-namespace:SyncfusionTest"
             x:Class="SyncfusionTest.DerivedBorderView">

    <!--For some reason i need to set the below content in base.Content property-->
    <local:BaseBoxView.Content>
        <Grid>
            <Grid>

            </Grid>
        </Grid>
    </local:BaseBoxView.Content>
</local:BaseBoxView>

Posts

  • ********* Found solution *************

    Have to set ContentView.Content instead of BaseBoxView.Content.

    <ContentView.Content>

            </Grid>
        </Grid>
    

    </ContentView.Content>

  • @saravananMadheswaran said:
    ********* Found solution *************

    Have to set ContentView.Content instead of BaseBoxView.Content.

    <ContentView.Content>

            </Grid>
        </Grid>
    

    </ContentView.Content>

Sign In or Register to comment.