wpf 绑定到设置中定义的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/845030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:24:33  来源:igfitidea点击:

Bind to a value defined in the Settings

wpfbindingappsettings

提问by CSharper

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

在 WPF 中,我可以将绑定与设置中定义的值结合使用吗?如果可能,请提供样本。

回答by CSharper

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

首先,您需要添加一个自定义 XML 命名空间,用于设计定义设置的命名空间:

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

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

然后,在您的 XAML 文件中,使用以下语法访问默认设置实例:

{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?

来源:WPF - 如何将控件绑定到设置中定义的属性?



Note: As pointed out by @Daniel and @nabulke, don't forget to set Access Modifierof your settings file to Publicand Scopeto User

注意:正如@Daniel 和@nabulke 所指出的,不要忘记将设置文件的Access Modifier设置为PublicScope设置为User

回答by Thomas Levesque

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/

更多细节在这里:http: //www.thomaslevesque.com/2008/11/18/wpf-binding-to-application-settings-using-a-markup-extension/

回答by Christopher Kyle Horton

@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 Settingscould not be found in the MyApp.Propertiesnamespace, which would not go away even after rebuilding.

@CSharper 的答案不适用于我用 VB.NET 编码的 WPF 应用程序(不是 C#,显然与 99.999% 的其他 WPF 应用程序不同),因为我收到了一个持续的编译器错误,抱怨SettingsMyApp.Properties命名空间中找不到,这不会即使在重建后也离开。

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

在网上进行了大量搜索之后,对我有用的是改为使用local在我的应用程序的主窗口 XAML 文件中默认创建的XAML 命名空间:

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

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

...并使用类似以下内容通过它绑定到我的设置(MyBooleanSetting我在我的项目属性中定义的类型Boolean和范围用户的设置,使用默认的 Friend 访问修饰符):

<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.Closingevent for your MainWindow.xaml.vbfile).

...在您的代码隐藏中的某个地方(例如在Me.Closing您的MainWindow.xaml.vb文件的事件中)。

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

(归功于此Visual Studio 论坛帖子的灵感;请参阅 Muhammad Siddiqi 的回复。)

回答by nPcomp

Here's how I bind the UserSettings:

这是我绑定 UserSettings 的方法:

Generate a dependency variable by typing propdpand then tab twice.

通过键入propdp然后 Tab 两次来生成依赖变量。

    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 userSettingsby:

现在,您可以userSettings通过以下方式绑定:

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();