Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

WPF : Binding to global App properties

5.00/5 (6 votes)
9 Apr 2009CPOL1 min read 56.5K  
I was working on something for an upcoming article that I am writing where I am messing around with 3D, and I was having a small issue with a VisualBrush (something to do with Freezable, I never did quite figure it out and came up with a better solution), but along the way of trying [...]

I was working on something for an upcoming article that I am writing where I am messing around with 3D, and I was having a small issue with a VisualBrush (something to do with Freezable, I never did quite figure it out and came up with a better solution), but along the way of trying to figure that out, one of the ideas I had was to use a globally available Application property and Bind to that. This is something I had not done before. Luckily it is very simple.

The only thing that you have to be aware of is that you can not use the normal DP GetValue() SetValue() syntax in Application inheriting objects, as these are added further up the inheritance tree (DependencyObject) where as Application inherits from DispatcherObject, so does not have these methods.

So we must simply use CLR properties.

Here is a small example.

My App class looks like this

C#
 1:  public partial class App : Application
 2:  {
 3:      private String someText="default";
 4:
 5:      public String SomeText
 6:      {
 7:          get { return this.someText; }
 8:          set { this.someText = value; }
 9:      }
10:  }

And I have a small XAML Window which wants to bind to one of the App properties, we simply use the following Binding, note the x:Static markup extension.

C#
 1:  <Window x:Class="BindingToApp.Window1"
 2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4:      Title="Window1" Height="300" Width="300">
 5:
 6:      <StackPanel Orientation="Vertical">
 7:          <TextBlock Margin="5" TextWrapping="Wrap"
 8:                     Text="Shows how to bind to App level properties,
 9:                     type below and then click button"/>
10:          <TextBox x:Name="txt" Margin="5" >
11:              <TextBox.Text>
12:                  <Binding Path="SomeText"
13:                           Source="{x:Static Application.Current}"/>
14:              </TextBox.Text>
15:          </TextBox>
16:          <Button x:Name="BtnShowAppSetting" Margin="5"
17:                  Content="What is App Value Now"
18:                  Click="BtnShowAppSetting_Click"/>
19:      </StackPanel>
20:
21:  </Window>

And here is the code behind

C#
 1:      public partial class Window1 : Window
 2:      {
 3:          public Window1()
 4:          {
 5:              InitializeComponent();
 6:          }
 7:
 8:          private void BtnShowAppSetting_Click(object sender, RoutedEventArgs e)
 9:          {
10:              MessageBox.Show(String.Format("App value for SomeText property is {0}",
11:                  (Application.Current as App).SomeText));
12:          }
13:      }

And here is the result when run

props-thumb.jpg

Here is a link to this demo project in case you need to try it : bindingtoapp.zip

Along the way I also found a very interesting post that illustrated how to bind directly to the Settings files that can be added to a VS project. That is pretty useful for persisting user specific data. Here is a link to that article

http://dedjo.blogspot.com/2008/04/quick-wpf-tip-how-to-bind-to-wpf.html

Enjoy

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)