i am using that code on button click-
Mainwindow.xaml
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:pp.Pages"
Title="MainWindow" MinHeight="350" MinWidth="525"
Background="Transparent">
Page1.xaml
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:v="clr-namespace:pp.Pages"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="FileView">
Page1.xaml.cs
private void Btn_Click(object sender, RoutedEventArgs e)
{
MainWindow mw=new MainWindow();
mw.textbox1.Text="hello";
}
Piyush PansuriyaPosted Feb 6, 2015, 5:17 AM
as per your code, you are initializing new MainWindow(). So its basically two mainwindow you have been created.
Solution :
When MainWindow Load set this in GlobleClass (Static Common Class for all). And give textbox value to that instance.
i.e.
public static class GlobleClass
public static MainWindow uc_MainWindow;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
GlobleClass.uc_MainWindow = this;
}
Page1.xaml.cs
private void Btn_Click(object sender, RoutedEventArgs e)
{
GlobleClass.uc_MainWindow.textbox1.Text="hello";
}
Piyush PansuriyaPosted Feb 6, 2015, 11:25 PM
Same thing you are repeating again.!! You are initializing class 2 times. so its two different classes.
if you want to pass value to page2, you have to declare variable in page2.
page1.xaml.cs
public page1()
{
InitializeComponent();
page2.content = "Hello";
}
page2.xaml.cs
public string content { get; set; }
public page2()
{
InitializeComponent();
this.textbox=content;
}
Suraj SararfPosted Feb 6, 2015, 9:29 AM
i have one more problem in property. i set some value in property on page1 and when call it in page2 then property doesn't have any value.
so what should i do?
variable.cs
page1.xaml.cs
public page1()
{
InitializeComponent();
Variable v=new Variable();
v.content="hello";
}
page2.xaml.cs
public page2()
{
InitializeComponent();
Variable v=new Variable();
this.textbox=v.content;
}
But textbox doesn't have any value.