简体   繁体   English

如何将用户设置 (Properties.Settings.Default) 绑定到 WPF 控件?

[英]How can I bind user settings (Properties.Settings.Default) to WPF controls?

I want to create a WPF window for user settings, and bind the controls to the user settings which are defined in Properties.Settings.Default.我想为用户设置创建一个 WPF window,并将控件绑定到 Properties.Settings.Default 中定义的用户设置。 Sounds pretty straight forward, like something that's done in almost all apps.听起来很简单,就像几乎所有应用程序中所做的一样。 However it seems I cannot achieve a bi-directional binding, ie the controls show the values, but the user input is not written back into the settings object. Here's part of the XAML with a canvas which defines a data context and contains some controls.然而,我似乎无法实现双向绑定,即控件显示值,但用户输入不会写回设置 object。这是 XAML 的一部分,其中 canvas 定义了数据上下文并包含一些控件。

<Canvas x:Name="CanvasMain">
    <Canvas.DataContext>
        <Properties:Settings/>
    </Canvas.DataContext>
    <CheckBox x:Name="chkThingEnabled" Content="Thing" Height="20" Canvas.Left="19" Canvas.Top="64" Width="106" IsChecked="{Binding ThingEnabled}"/>
    <TextBox Canvas.Left="19" Text="{Binding TestText}" Canvas.Top="104" Width="120"/>
</Canvas>

The settings are simply entered in the default settings grid of the project: ThingEnabled: Type = bool, Scope = User, Value = False TestText: Type = string, Scope = User, Value = "TEST"只需在项目的默认设置网格中输入设置: ThingEnabled:Type = bool,Scope = User,Value = False TestText:Type = string,Scope = User,Value =“TEST”

How can I make this data binding, so the changes to the control values are written into the bound properties of the settings?如何进行此数据绑定,以便将对控件值的更改写入设置的绑定属性中?

An expression like像这样的表达

<Properties:Settings/>

would create a new instance of the Settings class.将创建设置 class 的新实例。

In order to bind to the static Default property of the Settings class in the namespace YourApplication.Properties you would need a proper XAML namespace declaration and an appropriate Binding expression with the static property path in parentheses.为了绑定到命名空间YourApplication.PropertiesSettings class 的Default属性 static,您需要一个正确的 XAML 命名空间声明和一个适当的绑定表达式,括号中包含 static 属性路径。

You should also consider using a suitable layout Panel.您还应该考虑使用合适的布局面板。 Canvas is not the right thing here. Canvas 在这里不是正确的。

Something like this should work:这样的事情应该有效:

<Window ...
    xmlns:properties="clr-namespace:YourApplication.Properties">

    <StackPanel DataContext="{Binding Path=(properties:Settings.Default)}">

        <CheckBox IsChecked="{Binding ThingEnabled}" .../>
        <TextBox Text="{Binding TestText}" .../>
    </StackPanel>
</Window>

You need to call Properties.Settings.Default.Save() method to make the settings persistent sometime before closing your app.您需要调用Properties.Settings.Default.Save()方法使设置在关闭您的应用程序之前的某个时间持久化。 See How To: Write User Settings at Run Time with C# .请参阅如何:使用 C# 在运行时编写用户设置

In practice, this approach is not so convenient because the path of user settings file is dependent on the version number of your app and it will not be passed to newer version when you change the version.实际上,这种方法并不方便,因为用户设置文件的路径取决于您的应用程序的版本号,并且在您更改版本时不会将其传递到较新的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM