Bind to a value defined in the Settings

Moheb picture Moheb · May 10, 2009 · Viewed 45.5k times · Source

In WPF, Can I use binding with values defined in Settings? If this is possible, please provide a sample.

Answer

CSharper picture CSharper · May 10, 2009

First, you need to add a custom XML namespace that will design the namespace where the settings are defined:

xmlns:properties="clr-namespace:TestSettings.Properties"

Then, in your XAML file, access the default settings instance using the following syntax:

{x:Static properties:Settings.Default}

So here is the final result code:

<ListBox x:Name="lb"
         ItemsSource="{Binding Source={x:Static properties:Settings.Default},
                               Path=Names}" />

Source: WPF - How to bind a control to a property defined in the Settings?


Note: As pointed out by @Daniel and @nabulke, don't forget to set Access Modifier of your settings file to Public and Scope to User