Forum Xamarin.Forms

Referencing a children from a custom control

AdhamAliAdhamAli USMember ✭✭
edited August 2018 in Xamarin.Forms

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>
...

Best Answer

  • AdhamAliAdhamAli US ✭✭
    Accepted Answer

    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.

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    That looks fine, as long as MainBody is a public property of ControlName.

  • AdhamAliAdhamAli USMember ✭✭

    Main body is the name of the inner content view and it's a private field

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Then change it to a public property.

  • AdhamAliAdhamAli USMember ✭✭
    edited August 2018

    This is what i did and it didn't work:

    public ContentView MainBody
            {
                get => Body;
                set => Body = value;
            }
    

    Body is the private field for the control.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    What do you mean by "it didn't work"? Is the setter getting hit? Are you properly laying out MainBody in your LayoutChildren method?

  • AdhamAliAdhamAli USMember ✭✭

    Did you try it on your machine and it worked?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I've done it a dozen times with custom views. I don't know if it will make a difference, but I always back mine with Bindable Properties.

  • AdhamAliAdhamAli USMember ✭✭
    Accepted Answer

    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.

Sign In or Register to comment.