MVVM data binding

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

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;
       }
   }
}
 
If you expect data to flow between windows via a Global object then you need a single instance bound to both. You've got multiple Global objects so changes to one have no effect on the others. You create one instance when you create the main window, then another instance each time you click the Button on the main window and then another instance each time you create a formatting window. Why would you expect to be able to get data out of a Global object that you just created when you put data into a different instance that you created somewhere else? That's like writing in a notebook and then going to a shop and buying another notebook of the same type and being surprised that what you wrote is not in it.
 
Lol OK, good point and good illustration. I wasn't sure what the scope of an object would be within a click event method or window class. I assumed that they would all be linked somehow. Through trial and error i just began adding objects wherever i needed the compiler to allow me to refer to a property of the class.

So if i only need one object and refer to that one rather than multiple different ones, where do i put the object so that all window classes and controls can access it and the values stored within?

Also, you mention binding a window to a n object. is that the data.context property? when i have tried to go down that road and set a window's data context, intellisense seemed to only ever suggest the class rather than the object as available option.

I appreciate your help.
 
Back
Top Bottom