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

c.hum
  • 35
  • 8
  • The question is a bit abstract. Could you show more information? – jjw Dec 21 '19 at 01:30
  • @jjw i tries to explain the situation a bit better – c.hum Dec 21 '19 at 01:33
  • I think this article to help you. https://stackoverflow.com/questions/15122299/how-do-i-get-a-page-instance-from-a-frame – jjw Dec 21 '19 at 02:28
  • If you get the instance of the Page then you can retrieve the var of the Page – jjw Dec 21 '19 at 02:39
  • @jjw so once i do this it outputs the instance WpfApp3.CountdownScreen. im assuming this is correct? where do i go from this to be able to get the var – c.hum Dec 21 '19 at 03:02
  • I would show you an example. – jjw Dec 21 '19 at 03:16
  • You definitely need to read up [`DataContext`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.frameworkelement.datacontext#remarks) and [Microsoft Docs: Data Binding](https://learn.microsoft.com/en-us/windows/uwp/data-binding/). This is the way to solve your problem. – BionicCode Dec 21 '19 at 08:32

1 Answers1

0

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.

jjw
  • 282
  • 3
  • 20
  • You definitely need to read up [`DataContext`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.frameworkelement.datacontext#remarks) and [Microsoft Docs: Data Binding](https://learn.microsoft.com/en-us/windows/uwp/data-binding/). – BionicCode Dec 21 '19 at 08:32