Naxoscompact
Member
- Joined
- Mar 26, 2020
- Messages
- 5
- Programming Experience
- Beginner
So I'm relatively new to c# and I'm struggling with a particular issue.
I want the text inputted by the user into a text box in one window to be the text value of a textblock in another window automatically.
ie. user types something in TextBox x:name Textinput in MainWindow and that text appears in TextBlock x:name TextTarget when FormattingWindow opens.
I have tried with a number of different methods with no success. i would accept any method to solve this problem, but after doing a lot of reading around this hoping to find an answer, i understand that MVVM is the way to go. Im still getting to grips with that, but as i understand, it a separate class View deals with all properties. Using this form i guess my question is how do i pass a property to one window, change it there to the value of TextInput and then call this newly changed property in the new window to set it as the TextBlock text value.
code so far...
I want the text inputted by the user into a text box in one window to be the text value of a textblock in another window automatically.
ie. user types something in TextBox x:name Textinput in MainWindow and that text appears in TextBlock x:name TextTarget when FormattingWindow opens.
I have tried with a number of different methods with no success. i would accept any method to solve this problem, but after doing a lot of reading around this hoping to find an answer, i understand that MVVM is the way to go. Im still getting to grips with that, but as i understand, it a separate class View deals with all properties. Using this form i guess my question is how do i pass a property to one window, change it there to the value of TextInput and then call this newly changed property in the new window to set it as the TextBlock text value.
code so far...
View class (named Global):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp_trial_bindingSO
{
class Global
{
string memberid;
public string MemberID
{
get { return memberid; }
set { memberid = value; }
}
}
}
MainWindow.Xaml:
<Window x:Class="WpfApp_trial_bindingSO.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:local="clr-namespace:WpfApp_trial_bindingSO"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox x:Name="Textinput"
Height="150"
Width="150"
BorderThickness="3"
/>
<Button x:Name="buttonNXT" Content="Next Page" HorizontalAlignment="Left" Margin="120,228,0,0" VerticalAlignment="Top" Width="75" Click="buttonNXT_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp_trial_bindingSO
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Global myglobal = new Global();
this.DataContext = myglobal;
}
private void buttonNXT_Click(object sender, RoutedEventArgs e)
{
Global myglobal = new Global();
myglobal.MemberID = Textinput.Text;
FormattingWindow newfm = new FormattingWindow();
newfm.Show();
this.Close();
}
}
}
FormattingWindow.xaml:
<Window x:Class="WpfApp_trial_bindingSO.FormattingWindow"
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:local="clr-namespace:WpfApp_trial_bindingSO"
mc:Ignorable="d"
Title="FormattingWindow" Height="450" Width="800">
<Grid>
<TextBlock x:Name="TextTarget" Height="150" Width="150" />
</Grid>
</Window>
FormattingWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp_trial_bindingSO
{
/// <summary>
/// Interaction logic for FormattingWindow.xaml
/// </summary>
public partial class FormattingWindow : Window
{
public FormattingWindow()
{
InitializeComponent();
Global myglobal = new Global();
this.DataContext = myglobal;
TextTarget.Text = myglobal.MemberID;
}
}
}