我正在将一个做羊毛剪切比赛计分的Windows Forms项目(不要问,这在新西兰是一项非常受欢迎的运动)从VB.NET重写为WPF C#。我遇到了一个问题,似乎无法解决。
我有两个窗口。一个是源窗口,在其中输入内容(例如当前事件名称),另一个窗口会以闪现的方式显示此信息以投影到屏幕上(因此会在第二个监视器上),同时还会通过网络上的XML传输其他数据。我已经将其设置为MVVM,ViewModel和Model作为独立的项目。
在我的主窗口中,我可以很好地绑定控件,并且如果我在一个文本框中键入,则如果它与相同的内容绑定,则它会立即出现在另一个文本框中。 然而,在第二个窗口中,我将控件绑定到相同的位置,但它没有更新。
我已经困扰了一个星期,网络上的每个示例都展示如何在一个窗口上执行操作,我已经成功地完成了这项工作,但缺少两个窗口的示例。
以下是我的代码...
这是我的ViewModel项目中的内容:
然后是XAML。
我有两个窗口。一个是源窗口,在其中输入内容(例如当前事件名称),另一个窗口会以闪现的方式显示此信息以投影到屏幕上(因此会在第二个监视器上),同时还会通过网络上的XML传输其他数据。我已经将其设置为MVVM,ViewModel和Model作为独立的项目。
在我的主窗口中,我可以很好地绑定控件,并且如果我在一个文本框中键入,则如果它与相同的内容绑定,则它会立即出现在另一个文本框中。 然而,在第二个窗口中,我将控件绑定到相同的位置,但它没有更新。
我已经困扰了一个星期,网络上的每个示例都展示如何在一个窗口上执行操作,我已经成功地完成了这项工作,但缺少两个窗口的示例。
以下是我的代码...
这是我的ViewModel项目中的内容:
namespace SheepViewModel
{
public class SheepViewModel : INotifyPropertyChanged
{
private string _CurrentEventName;
static SheepViewModel _details;
public string CurrentEventName
{
get { return _CurrentEventName; }
set
{
_CurrentEventName = value;
OnPropertyChanged("CurrentEventName");
}
}
public static SheepViewModel GetDetails()
{
if (_details == null)
_details = new SheepViewModel();
return _details;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string prop)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
Console.WriteLine("Test");
}
}
}
然后我有一个主窗口,除了一行代码打开我们将要讲到的第二个窗口之外,实际上没有其他真正的代码...
public MainWindow()
{
ScoreScreen SW = new ScoreScreen();
SW.Show();
InitializeComponent();
}
然后是XAML。
<Window x:Class="Sheep_Score_3._1.MainWindow"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
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:vm="clr-namespace:SheepViewModel;assembly=SheepViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="433.689" Width="941.194">
<Window.DataContext>
<vm:SheepViewModel/>
</Window.DataContext>
<Window.Resources>
<Grid Margin="0,0,0,0">
<TextBox x:Name="CurrentEventName" Height="23" Margin="131.01,163.013,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Width="327.151" Text="{Binding CurrentEventName, Mode=TwoWay}"/>
<TextBox Text="{Binding CurrentEventName, Mode=TwoWay}" Margin="39.605,0,0,108.567" Height="49.111" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="399" />
</Grid>
以上代码都可以正常工作,如果我在第一个文本框中输入文本,它会出现在第二个文本框中。如果我在通知部分添加console.writeline,则可以看到它正在更新。
现在我添加了第二个窗口,设置完全相同...
<Window x:Class="Sheep_Score_3._1.ScoreScreen"
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:vm="clr-namespace:SheepViewModel;assembly=SheepViewModel"
mc:Ignorable="d"
Title="ScoreScreen" Height="300" Width="300">
<Window.DataContext>
<vm:SheepViewModel/>
</Window.DataContext>
<Grid>
<TextBox x:Name="textBlock" HorizontalAlignment="Left" Margin="79.374,116.672,0,0" TextWrapping="Wrap" Text="{Binding CurrentEventName, Mode=TwoWay}" VerticalAlignment="Top"/>
</Grid>
再次说明,这里没有实际的代码。
奇怪的是,如果我将此控件设置为双向并在其中输入文本,我可以看到它触发了相同的通知部分,但它没有更新另一个窗口。
我不确定我错过了什么,请帮助指引我正确的方向,谢谢。
SW.DataContext = DataContext
,并将<Window.DataContext>
中的无聊代码留在MainWindow中,在其他窗口的XAML中指定d:DataContext
属性。或者只需在两个窗口中指定d:DataContext
,但构造函数应保持@Will编写的不变。 - Adam L. S.