绑定到设置中定义的值

发布于 2024-07-21 03:41:43 字数 51 浏览 9 评论 0原文

在 WPF 中,我可以使用与“设置”中定义的值进行绑定吗? 如果可能的话,请提供样品。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聆听风音 2024-07-28 03:41:43

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

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

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

{x:Static properties:Settings.Default}

因此,这是最终结果代码:

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

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


注意:正如 @Daniel 和 @nabulke 所指出的,不要忘记设置 设置文件的访问修饰符Public范围User

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

流绪微梦 2024-07-28 03:41:43

上面的解决方案确实有效,但我发现它非常冗长...您可以使用自定义标记扩展,可以像这样使用:

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

这是此扩展的代码:

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;
    }
}

更多详细信息在这里:http://www.thomaslevesque.com/2008 /11/18/wpf-绑定到应用程序设置-使用标记扩展/

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/

我不会写诗 2024-07-28 03:41:43

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

经过大量在线搜索后,对我来说有效的方法是使用在我的应用程序主窗口 XAML 文件中默认创建的 local XAML 命名空间:

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

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

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

确保设置实际保存,一定要打电话

MySettings.Default.Save()

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

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

@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.)

摘星┃星的人 2024-07-28 03:41:43

以下是我绑定 UserSettings 的方法:

通过键入 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));

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

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

并确保在更改或退出时保存 UserSettings:

UserSettings.Default.Save();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文