嗯,这会很棘手。我要做的是用 Grid 创建一个 UserControl,然后将一个 UserControl 放在 Window1 中,另一个放在 Window2 中。但是要同步 Window1-Grid 和 Window2-Grid 的状态,您必须将它们绑定到同一个对象。
编辑:在这里,为您准备了一个示例
第 1 步:将Grid 放入UserControl,以便重复使用。
请注意,我将绑定上的UpdateSourceTrigger 属性设置为PropertyChanged,这会在用户键入时更新源对象,因此我们将看到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
我刚刚为演示创建了一个名为 person 的随机对象。
注意:您必须在您的对象上实现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
}
无论如何,我希望这对你有所帮助。如果您遇到困难,我上传了项目的压缩版本。 http://www.mediafire.com/?yv84xbben6tjdy7
祝你好运。