Melon NG
Melon NG

Reputation: 2984

How can I binding element cross window?

There are two windows: MainWindow and Window1.

Here is the XAML of MainWindow:

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <TextBox x:Name="TB"></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
    </Grid>
</Window>

Here is Code-behind of MainWindow:

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 WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Window1 W = new Window1(this);
            W.Show();
        }
    }
}

Here is the XAML of Window1:

<Window x:Class="WpfApp1.Window1"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <TextBox Text="{Binding Text,ElementName=TB,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>
</Window>

Here is Code-behind of Window1:

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 WpfApp1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1(MainWindow MW)
        {
            InitializeComponent();
            this.DataContext = MW;
        }
    }
}

I wanna bind the TextBox of Window1 to the TextBox which X:Name="TB" in MainWindow.

How can I do this? Why my codes do not work?

Would you please help me? Thank you.

Upvotes: 0

Views: 148

Answers (2)

Henka Programmer
Henka Programmer

Reputation: 747

Create a ViewModel and share it with both Windows DataContext

ViewModel:

// NB: Not the Perfect or complete solution to Implement INotifyPropertyChanged
// Don't use it in Production Code.
public class ViewModel : INotifyPropertyChanged
{
    #region Notify Property Changes
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string pname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pname));
    }
    #endregion

    private string _Text;

    public string Text
    {
        get { return _Text; }
        set
        {
            if (value != _Text)
            {

                _Text = value;
                OnPropertyChanged("Text");
            }
        }
    }

}

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    private ViewModel ViewModel;
    public MainWindow()
    {
        ViewModel = new ViewModel();
        DataContext = ViewModel;
        InitializeComponent(); 
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Window1 W = new Window1() { DataContext = ViewModel};
        W.Show();
    }

}

MainWindow.xaml

<Window x:Class="WpfApp1.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:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
    <TextBox Text="{Binding Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</Grid>

Upvotes: 0

mm8
mm8

Reputation: 169150

Why my codes do not work?

There is no element named "TB" to bind to in Window1.

If you add a property to MainWindow, you could bind to this one:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Window1 W = new Window1(this);
        W.Show();
    }


    public string TheText
    {
        get { return TB.Text; }
        set { TB.Text = value; }
    }
}

XAML in Window1:

<TextBox Text="{Binding TheText, UpdateSourceTrigger=PropertyChanged}" />

You may want to create a view model that you share between the windows rather than injecting Window1 with a reference to MainWindow but that's antother story.

Upvotes: 2

Related Questions