1
Answer

Passing variables between wpf pages

Rich

Rich

13y
4.9k
1
I've just recently learned how to pass variables between wpf pages via static members.  What is the obvious benefit, if any, passing variables using objects?  Thanks in advance.

Rich
Answers (1)
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
As in Windows Forms, there are a number of ways to pass data between windows in WPF.

One way (possibly the one you're thinking of) is for a window to expose a reference to itself via a static field or property, which other windows can then use to access controls on that window.

A more object-oriented way is to use events which notify one window when data changes on another window and use the eventhandler arguments to pass the data.

Which of these is preferable depends on the circumstances.

The argument for using events is that they provide a more robust, scaleable and easier to maintain solution. However, unless one window is opening another, you still need a way to obtain a reference to the second window (and the controls on it) in order to subscribe to the appropriate events in the first place, So events on their own may not offer a complete solution.