Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML Binding: binding to properties of a "global" object

I would like to have one (global, singleton) object in my application that exposes a number of dependency properties. I would like to bind values in XAML to these dependency properties. How can I achieve this so that the syntax of my XAML binding is as simple as possible (in other words, not constantly worrying about RelativeSource, AncestoryType, etc).

like image 563
anthony Avatar asked Dec 07 '11 00:12

anthony


People also ask

How does binding work in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is two way binding in WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.


1 Answers

You can use the x:Static markup extension to bind directly to your Singleton, as it's a static property.

For example, if your singleton had a property named "Foo":

<TextBox Text="{x:Static local:YourSingleton.Instance.Foo}" />
like image 186
Reed Copsey Avatar answered Oct 04 '22 15:10

Reed Copsey