嗯,這將是棘手。我要做的是用Grid創建一個UserControl,然後在Window1中放置一個UserControl,在Window2中放置另一個UserControl。但是爲了同步Window1-Grid和Window2-Grid的狀態,你必須將它們綁定到同一個對象上。
編輯:在這裏,炮製了一個例子,你
步驟1:把Grid
成UserControl
因此它可以重複使用。
通知我設置的結合PropertyChanged
,這將更新源對象爲用戶鍵入UpdateSourceTrigger
屬性,所以我們會看到Window2
變化,因爲它們是在Window1
發生。
CommonGrid.xaml
<UserControl x:Class="WindowSync.CommonGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5">
<TextBlock Text="Name: " />
<TextBox Width="200" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5">
<CheckBox IsChecked="{Binding IsAdmin, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text=" Is Admin" />
</StackPanel>
</Grid>
</UserControl>
步驟2:把UserControl
成所需的窗戶。
注意:您必須引用UserControl
所在的命名空間,在這種情況下命名空間爲WindowSync
,此行允許我們使用命名空間xmlns:app="clr-namespace:WindowSync"
。
Window1.xaml
<Window x:Class="WindowSync.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WindowSync"
Title="Window 1" Height="200" Width="400">
<app:CommonGrid x:Name="Window1Grid" />
</Window>
Windo1.xaml.cs
public Window1()
{
InitializeComponent();
Window1Grid.DataContext = Person.User; // bind the UserControl in Window1 to the an object
new Window2().Show(); // create an instance of window 2 and show it
}
Window2.xaml
<Window x:Class="WindowSync.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WindowSync"
Title="Window 2" Height="200" Width="400">
<app:CommonGrid x:Name="Window2Grid" />
</Window>
Window2.xaml.cs
public Window2()
{
InitializeComponent();
Window2Grid.DataContext = Person.User; // bind the UserControl in Window2 to the same object
}
Person.cs
我剛剛創建了一個叫做人的示範隨機對象。
注意:您必須在您的對象上實現接口INotifyPropertyChanged
,並在事件發生變化時引發適當的PropertyChanged
事件,這就是讓我們同步這兩個網格。Window1改變了一些東西,PropertyChanged
事件被激發,Window2
拿起它並進行更改。
public class Person : INotifyPropertyChanged
{
public static Person User = new Person();
#region Name
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
#endregion
#region IsAdmin
private bool _IsAdmin;
public bool IsAdmin
{
get { return _IsAdmin; }
set
{
_IsAdmin = value;
OnPropertyChanged("IsAdmin");
}
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
#endregion
}
Anywyas,我希望這可以幫助你。如果您遇到問題,我會上傳項目的壓縮版本。 http://www.mediafire.com/?yv84xbben6tjdy7
祝你好運。
更新了我的答案,添加了演示和一些代碼,希望它有幫助。 –