Search code examples
wpfuser-interfacebuttonframe

How to retrieve data from a page in a frame on button click c#


private void btnEdit(object sender, RoutedEventArgs e)
        {
}

in this button event i want to be able to retrieve two variables from a different xaml which is displayed in the frame of this window. I have no clue how to do this

I have a mainscreen with two buttons and a frame. in this frame i display a page. when this button in the above code is clicked i want to be able to get two variables from the pages textblock on the page. how would i go about this?

i basically need to collect data from the frame to be used in this event


Solution

  • I don't know that your project structure and what you try to do. So I would show you an example on the basis of I understood.

    I created the below Page. Page name is "TestPage.xaml"

    <Page x:Class="StackOverFlowAnswers.TestPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:local="clr-namespace:StackOverFlowAnswers"
          mc:Ignorable="d" 
          d:DesignHeight="450" d:DesignWidth="800"
          Title="TestPage">
    
        <Grid>
            <TextBlock x:Name="textBlock"/>
        </Grid>
    </Page>
    

    I created MainWindow that has two button with one frame as below. Now the source of the Frame is "TestPage.xaml".

    <Window x:Class="StackOverFlowAnswers.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:attached="clr-namespace:Parse.WpfControls.AttachedProperties"
            xmlns:local="clr-namespace:StackOverFlowAnswers"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
            <Style x:Key="ConvertableTextBox" TargetType="TextBox">
                <Setter Property="attached:CharacterConvertBehavior.ConvertEnable" Value="True"/>
            </Style>
        </Window.Resources>
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <StackPanel Orientation="Horizontal" Grid.Row="0">
                <Button Width="50" Margin="0 0 10 0" Click="Button_Click"/>
                <Button Width="50" Margin="0 0 10 0"/>
            </StackPanel>
    
            <Grid Grid.Row="1">
                <Frame x:Name="mainFrame" Source="TestPage.xaml">
                </Frame>
            </Grid>
        </Grid>
    </Window>
    

    Now when the button is clicked would move to the event method of the below.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var page = (TestPage)this.mainFrame.Content;
    
        page.textBlock.Text = "test!!!";
    }
    

    In the above way, you could access (retrieve) the var of the Page.