Bind to a value defined in the Settings
Asked Answered
H

4

86

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

Hargis answered 10/5, 2009 at 9:55 Comment(0)
T
148

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

Temptation answered 10/5, 2009 at 9:57 Comment(2)
I have found that this method only works if the settings file is marked as public access modifier. shortfastcode.blogspot.com/2009/12/…Enrobe
In order to save the value using MyApp.Properties.Settings.Default.Save(), the setting scope has to be set to User.Carranza
S
32

The solution above does work, but I find it quite verbose... you could use a custom markup extension instead, that could be used like this :

<ListBox x:Name="lb" ItemsSource="{my:SettingBinding Names}" />

Here is the code for this extension :

public class SettingBindingExtension : Binding
{
    public SettingBindingExtension()
    {
        Initialize();
    }

    public SettingBindingExtension(string path)
        :base(path)
    {
        Initialize();
    }

    private void Initialize()
    {
        this.Source = WpfApplication1.Properties.Settings.Default;
        this.Mode = BindingMode.TwoWay;
    }
}

More details here : http://www.thomaslevesque.com/2008/11/18/wpf-binding-to-application-settings-using-a-markup-extension/

Sydelle answered 10/5, 2009 at 23:19 Comment(2)
This is a great extension, but unfortunately you lose Resharper intellisense. Not worth the tradeoff IMHO, though it should be noted that I am a hopeless R# fanboy :)Romanticize
@OhadSchneider, indeed. I use ReSharper myself, but rarely for XAML, because I don't find it as good as in C#...Sydelle
C
10

@CSharper's answer did not work for my WPF application coded in VB.NET (not C#, unlike apparently 99.999% of other WPF applications), as I got a persistent compiler error complaining that Settings could not be found in the MyApp.Properties namespace, which would not go away even after rebuilding.

What worked instead for me, after much searching online, was to instead use the local XAML namespace created by default in my application's main window XAML file:

<Window
    <!-- Snip -->
    xmlns:local="clr-namespace:MyApp"
    <!-- Snip -->
><!-- Snip --></Window>

...and bind to my settings through it using something like the following (where MyBooleanSetting is a setting I defined in my project properties of type Boolean and scope User, with the default Friend access modifier):

<CheckBox IsChecked="{Binding Source={x:Static local:MySettings.Default}, Path=MyBooleanSetting, Mode=TwoWay}"
          Content="This is a bound CheckBox."/>

To ensure the settings are actually saved, be sure to call

MySettings.Default.Save()

...somewhere in your code-behind (such as in the Me.Closing event for your MainWindow.xaml.vb file).

(Credit to this Visual Studio forum post for the inspiration; see the reply by Muhammad Siddiqi.)

Contemporary answered 24/2, 2016 at 15:51 Comment(0)
F
0

Here's how I bind the UserSettings:

Generate a dependency variable by typing propdp and then tab twice.

    public UserSettings userSettings
    {
        get { return (UserSettings)GetValue(userSettingsProperty); }
        set { SetValue(userSettingsProperty, value); }
    }
    public static readonly DependencyProperty userSettingsProperty =
        DependencyProperty.Register("userSettings", typeof(UserSettings), typeof(MainWindow), new PropertyMetadata(UserSettings.Default));

Now, you can bind userSettings by:

Value="{Binding userSettings.SomeUserSettingHere, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

And make sure you save UserSettings when you change them or on Exit by:

UserSettings.Default.Save();
Fragrance answered 13/1, 2020 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.