User Settings in WPF 用户设置,在xaml中绑定等

It's been a while since my last post, so I figured I'd write something I just recently had to work on: user settings.

In many client applications, you quickly run into the need to store application or user settings. There are a few options on how to do that, some of which are more complex than others, and I'll provide links at the end on a other options I found. In this post, I'll describe one of the simpler solutions, which takes advantage of existing functionality in Visual Studio, makes use of data-binding in Xaml, and also allows you to retrieve or set settings through code.

Visual Studio has design-time support for application and user settings, described quite well in the MSDN documentation. The VS designer creates as Settings class and automatically generates properties based on the setting names. Moreover, the settings will work in a ClickOnce application. As for the limitations mentioned in the article, I would add that design-time implies you need to know the user settings ahead of time. Not usually a big problem, but it can be for more complex scenarios. For example, if your application supports plug-ins that have custom settings, you wouldn't necessarily know this ahead of time.

Assuming you’ve already created settings for your application, how could you go about making use of them in a WPF application? In order to access the properties in Xaml, you could add an entry in the application's resource dictionary:

<Application x:Class="SampleApp.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:properties="clr-namespace:SampleApp.Properties"
   Exit="OnExit"
   StartupUri="MainWindow.xaml">
     <Application.Resources>
         <properties:Settings x:Key="Settings" />
     </Application.Resources>
</Application>

If you had added a setting named Username as a string type, you could bind a TextBox in Xaml this way:

<TextBox Text="{Binding Source={StaticResource Settings}, Path=Default.Username}" />

As you enter text in the TextBox, the two-way binding automatically updates the Username setting, and similarly at startup, the TextBox will be populated with the most recently saved Username value.

Finally, in order to ensure your settings are properly saved, you could implement the OnExit handler declared above, or alternatively you could put the settings in a dedicated window and save at the appropriate time.

private void OnExit(object sender, ExitEventArgs e)
{
    Properties.Settings.Default.Save();

}

To access the current property value through code:

Properties.Settings.Default[“Username”]

Now, for the additional links I promised. This article offers a more complex and complete solution. Although it’s relatively heavy-weight, it adds support for specific WPF controls, such as saving column positions in a ListView.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF,可以通过以下方式在用户控件的XAML添加字典资源: 1. 创建资源字典文件: - 在用户控件的项目,右键单击“项目” -> “添加” -> “新建项”; - 在弹出的“添加新项”对话框,选择“资源字典”类型,指定名称并添加; - 打开资源字典文件,添加需要的资源,例如: ```xml <ResourceDictionary> <SolidColorBrush x:Key="MyBrush" Color="Red"/> </ResourceDictionary> ``` 2. 引用资源字典: 在用户控件的XAML,可以通过以下方式引用资源字典: - 在控件的根元素,添加一个“Resources”元素,例如: ```xml <UserControl x:Class="MyNamespace.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyNamespace"> <UserControl.Resources> <ResourceDictionary Source="MyResourceDictionary.xaml"/> </UserControl.Resources> <!-- 控件的内容 --> </UserControl> ``` 在这里,我们通过“Source”属性指定了资源字典文件的路径。 - 在控件的XAML,可以通过以下方式引用资源: ```xml <Rectangle Fill="{StaticResource MyBrush}" Width="100" Height="100"/> ``` 在这里,我们通过“StaticResource”关键字引用了资源字典的“MyBrush”资源。 注意,如果资源字典文件位于控件的项目,可以直接使用相对路径;如果资源字典文件位于其他项目,需要使用绝对路径或pack URI。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值