Search code examples
xamlxamarin.formscustom-controlssetcontentview

Referencing a children from a custom control


I'm creating a custom control by composing some elements in a ContentView. The ContentView has another nested ContentView called MainBody. How would i access and set MainBody when i'm using the control. This is what i want to achieve:

...
<controls.ControlName>
    <controls.ControlName.MainBody>
        <ContentView>
            ...
        </ContentView>
    </controls.ControlName.MainBody>
</controls.ControlName>
...

Solution

  • Actually this is what i came up with:

    public static readonly BindableProperty MainContentProperty = BindableProperty.Create("MainContent", typeof(View), typeof(DropShadowPanel), default(View));
    
            public View MainContent
            {
                get { return (View)GetValue(MainContentProperty); }
                set { SetValue(MainContentProperty, value); OnPropertyChanged(); }
            }
    
            public DropShadowPanel ()
            {
                InitializeComponent ();
                PropertyChanged += DropShadowPanel_PropertyChanged;
            }
    
            private void DropShadowPanel_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if(e.PropertyName == MainContentProperty.PropertyName)
                {
                    mainContent.Content = MainContent;
                }
            }
    

    And it works.