How to access the wpf form controls which is inside the frame?

Arivazhagan K 20 Reputation points
2023-07-13T07:09:51.6566667+00:00

Hi,

My WPF main page contains the frame control and it is loaded with another WPF sub page. Now I need to get Datagrid data, which is inside the sub page, from the main page when clicking the button which is inside the main form.

I am loading the subForm inside the Main forms frame by using below line.

nav_Frame.Source = new Uri("UserControls/subForm.xaml", UriKind.RelativeOrAbsolute);

Thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,626 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 25,196 Reputation points Microsoft Vendor
    2023-07-13T07:35:51.9666667+00:00

    Hi,@Arivazhagan K. Welcome Microsoft Q&A. You could try to use the following code.

    First, give the DataGrid a name in the subForm.xaml file:

    <DataGrid x:Name="myDataGrid" ...>
        <!-- DataGrid content -->
    </DataGrid>
    

    In the main page, after loading the subForm into the frame, you can access the DataGrid as follows:

    
    nav_Frame.Source = new Uri("UserControls/subForm.xaml", UriKind.RelativeOrAbsolute);
    
    
    var subForm = nav_Frame.Content as FrameworkElement; // Get the content of the frame
    var dataGrid = subForm?.FindName("myDataGrid") as DataGrid; // Find the DataGrid by its name
    
    
    if (dataGrid != null)
    {
        
        // For example, you can retrieve data from the DataGrid
        var data = dataGrid.ItemsSource; // Assuming you have set the ItemsSource for the DataGrid
        // Do something with the data
    }
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    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