Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC

WPF : Binding to global App properties

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
9 Apr 2009CPOL1 min read 55.5K   9   4
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

This article was originally posted at http://sachabarber.net?p=432

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
QuestionDownload did not work Pin
Bob Ranck16-Jul-12 15:14
Bob Ranck16-Jul-12 15:14 
AnswerRe: Download did not work Pin
Sacha Barber16-Jul-12 18:58
Sacha Barber16-Jul-12 18:58 
GeneralRe: Download did not work Pin
Bob Ranck17-Jul-12 2:25
Bob Ranck17-Jul-12 2:25 
GeneralProblem with images... Pin
Rajesh Pillai11-Apr-09 2:38
Rajesh Pillai11-Apr-09 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.