maui How to access parent contentpage Grid in child page

dedeepya yarlagadda 50 Reputation points
2024-01-22T10:19:38.7966667+00:00

I have a grid in parentpage e.g InstructionPage and my page B is extending that InstructionPage and I want to access that grid and add a new row. How to do it.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,904 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 48,181 Reputation points Microsoft Vendor
    2024-01-23T03:09:47.5166667+00:00

    Hello,

    This is a matter of accessing parent class variables.

    Page elements are private variables after they are named in the XAML of the parent page, so you can't manipulate them directly in the child page.

    <Grid x:Name="testGrid">
    

    In this case, the easiest way to do this is to set up a public variable to make this control available to external access.

    public Grid gridView { get; set; }
    public MainPage()
    {
        InitializeComponent();
        gridView = testGrid;
    }
    

    After that, you can call this control in a class that inherits this page.

    public NewPage1()
    {
        InitializeComponent();
        this.gridView.AddRowDefinition(new RowDefinition());
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.