Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all

Hi have problem with databinding. I can bind data between controlls inside one window, but using same data between multiple windows is trivial.

Here is very simple program, hope you get the idea

Window1.xaml (could be the application MainWindow)

<window x:class="WPF_Template.Window1" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1. Able to be synchronized with window2" Height="145" Width="359">
    <grid>
        <stackpanel>
            <slider name="slider">
                Margin="4" Interval="1"
                TickFrequency="1"
                IsSnapToTickEnabled="True"
                Minimum="0" Maximum="100"/>
            <stackpanel orientation="Horizontal">
                <textblock width="Auto" horizontalalignment="Left">
                       VerticalAlignment="Center" Margin="4" 
                       Text="Gets and sets the value of the slider:" />
                <textbox width="40" horizontalalignment="Center" margin="4">
                 Text="{Binding 
                        ElementName=slider, 
                        Path=Value, 
                        Mode=TwoWay, 
                        UpdateSourceTrigger=PropertyChanged}" />
            </textbox></textblock></stackpanel>
            <Button Content="Show Window2" Click="Button_Click"></Button>
        </slider></stackpanel>
    </grid>
</window>


and Window1.xaml.cs

using System.Windows;

namespace WPF_Template
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window2 window2 = new Window2();
            window2.Show();
        }
    }
}


Now we have window1 and we want to create window2

<window x:class="WPF_Template.Window2" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2. Always synchronized with window1" Height="126" Width="355">
    <grid>
        <stackpanel>
            <slider name="slider" margin="4" interval="1" tickfrequency="1">
                IsSnapToTickEnabled="True"
                Minimum="0" Maximum="100"/>
            <stackpanel orientation="Horizontal">
                <textblock width="Auto" horizontalalignment="Left">
                       VerticalAlignment="Center" Margin="4" 
                       Text="Gets and sets the value of the slider:" />
                <textbox width="40" horizontalalignment="Center" margin="4">
                 Text="{Binding ElementName=slider,Path=Value,Mode=TwoWay, 
                        UpdateSourceTrigger=PropertyChanged}" />
            </textbox></textblock></stackpanel>
            <Button Content="Close" Click="Button_Click"></Button>
        </slider></stackpanel>
    </grid>
</window>


and and Window2.xaml.cs

using System.Windows;

namespace WPF_Template
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}


So the thing what I want to do is that Window1 and window2 are synchronized with each other. If you compile my code you will see that data is tied within windows individually and there is no interaction between windows.

Please give me advice!

Best recards

Damn, cannot post my full xaml- code. Hope you got idea anyway :doh:
Posted
Updated 1-Oct-17 22:34pm
v2

Have Window1 and Window2 use the same DataContext. That way the data is shared, and if one window updates something, it's instantly reflected in the other one.

It'll basically be one View-Model, two Views.
 
Share this answer
 
v2
Comments
Henry Minute 30-Oct-10 16:50pm    
Your answer was marked as answer but didn't have a 5???

So I gave you one. :)
paleGegg0 31-Oct-10 1:46am    
Sorry late update, just tested and it worked. You saved my day. Thank you :)
another way for WPF passing data between open windows.

in MainWindow.xaml.cs:
C#
((Window1)Application.Current.Windows[1]).textBlock1.Text="your text";
((Window1)Application.Current.Windows[1]).myProperty1=12;

or
C#
foreach (Window item in Application.Current.Windows)
{
   if (item.Name == "Window1")
   {
      (Window1)item).textBlock1.Text="your text";
      (Window1)item).myProperty1=12;
   }
}


Quote:
textblock1 control and myProperty1 in Window1.xaml
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900