I am rewriting my windows project, which catches events for shearing sheep (don't ask, its a huge sport in New Zealand) from vbnet to cp wpf and hit a problem that I cannot imagine to overcome.
I have two windows. One of them is the initial window in which you enter things (for example, the name of the current event), and the other window will display this information in flash mode for projection onto the screen (this will be on the second monitor) along with some other data via XML via net. I installed it as MVVM with ViewModel and Model as separate projects.
In my main window, I can bind controls in order, and if I enter one text field, it immediately appears in another text field if it is associated with the same. However, in the second window, I linked the control to the same, and it does not update.
Iβm going around in circles for a week, each example on the network shows how to do this in one window, which I got normally, but there are no two examples of windows.
Here is what I have ...
This is in my ViewModel project
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"); } } }
Then I have the main window, there is no real code except a line to open the second window, which we get ...
public MainWindow() { ScoreScreen SW = new ScoreScreen(); SW.Show(); InitializeComponent(); }
Then 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>
The code above works fine if I enter text in the first text box, it appears in the second text box. If I put console.writeline in the notification part, then I see how it hits and updates it.
Now I am adding a second window, setting up exactly the same ...
<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>
Again, there is no real code in this.
The strange thing is, if I do this management in two ways and type it, I see that it falls into the same notification section, but does not update another window.
I'm not sure what I'm missing here, so any help in pointing me in the right direction would be greatly appreciated.