gorokizu
gorokizu

Reputation: 2588

Updating two labels in two different WPF Windows

I've recently built a Volley Scoreboard Application in Windows Forms that I'm trying to convert to WPF.

My first Window has all the controls to manage the game, while the second one is the window that will be projected on the big screen.

In Windows Forms I was able to update both labels (example, home_score on both Main and Projected) from a single point in code, but I don't seem to find a way to do this in WPF.

Any chance I'm missing something? Relatively new to WPF.

Upvotes: 0

Views: 91

Answers (2)

dovid
dovid

Reputation: 6452

You need to:

  1. to name the label (TextBlock Probably) in the xaml (Name="ScoreLabel")
  2. keep a reference to the instance of the window.

after this:

otherWin.ScoreLabel.Text = "123";

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61339

If it makes sense to have both pages (Views) utilizing the same View Model; which it sounds like it might given your use case, then the answer is simple:

Bind both labels (really, TextBlocks) to the same property. When the property is updated; the binding engine will update both UIs.

If that isn't an option; the "master" View Model could invoke a method on the Model that would raise an event (ScoreUpdated). The "display" View Model would register for this event and update an appropriate property so that its View will pick up the change.

Upvotes: 1

Related Questions