I have application with WPF Ribbon and Grid. And I need to show this Grid not only in main application window but also on second window. This Grid contain a lot of elements like ToggleButtons, TextBoxes, Images.
Scheme of my application code looks like that:
<ribbon:RibbonWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
[...]
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="MainWindow"
x:Name="RibbonWindow"
Height="870" Width="1000">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ribbon:Ribbon x:Name="Ribbon">
[...]
</ribbon:Ribbon>
<Grid Background="#FF5C5C5C" Height="765" Width="986" Grid.Row="1" Margin="0,0,-2,-70" x:Name="MainGrid">
<ToggleButton/>
<TextBlock />
<ToggleButton/>
<Rectangle />
[...]
</Grid>
</Grid>
</ribbon:RibbonWindow>
MainGrid is the Grid that I want to show in second window. It can even be only view of this Grid. But when I change something in first window, like write something in TextBox or click on ToggleButton, I need to have it visible on second screen too.
Umm this is going to be tricky. What I would do is create a UserControl with the Grid and then put one UserControl in Window1 and another in Window2. But to synchronize the state of the Window1-Grid and the Window2-Grid you will have to bind them to the same object.
Edit: Here, cooked up an example for you
Step 1: Put the
Gridinto aUserControlso it can be reused.Notice I set the
UpdateSourceTriggerproperty on the binding toPropertyChanged, this updates the source object as the user is typing, so we will see changes inWindow2as they are happening inWindow1.CommonGrid.xaml
Step 2: Put the
UserControlinto the desired windows.Note: you have to reference the namespace that
UserControlis in, in this case the namespace isWindowSync, this line lets us user the namespacexmlns:app="clr-namespace:WindowSync".Window1.xaml
Windo1.xaml.cs
Window2.xaml
Window2.xaml.cs
Person.cs
I just created a random object called person for the demonstration.
Note: you have to implement the
INotifyPropertyChangedinterface on your object, and raise the appropriatePropertyChangedevent whenever something is changed, this is what let’s us synchronize the two grids. Window1 changes something, thePropertyChangedevent gets fired,Window2picks it up and makes the changes.Anywyas, I hope this helps you out. I uploaded a zipped version of the project if you get stuck. http://www.mediafire.com/?yv84xbben6tjdy7
Good luck.