I am using C# to create a WPF interface. The display shows current coordinates of a moving object. What I am doing is trigger a thread on an interval which calls a function that checks the current position of the moving object.
Note: The function which checks the current position is called from other class(in a different .cs file) and my interface is in a another window, which is the main window.
How can I show the coordinates in the window interface? Is there a way to assign a variable which is initialized in one class(.cs) to another label toolbar in the interface(.xaml)?
The simplest way to do this is to add a couple of
TextBlocks (orTextBoxif you prefer) and bind them to a property on a view model:The trick is that since you’re using a timer or a background thread, you have to synchronize the property change notifications so that they happen on your UI thread. So you could implement the
XCordproperty like:This check’s with the
Dispatcherto make sure you’re not trying to set the property from the wrong thread, and if you are, it usesBeginInvoketo do the work on the UI thread. That way, when thePropertyChangedevent fires, and the binding attempts to read the value and change the UI it does so from the UI thread rather than the background thread.