来自app.config的WPF绑定 [英] WPF binding from app.config

查看:84
本文介绍了来自app.config的WPF绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,
我是WPF的新手,我将在XAML代码中使用存储在app.config中的参数绑定标签的内容.我变通了,无法做到.
我在app.config中:

Dear All,
I''m new in WPF and I will bind in the XAML code the content of a label with a parameter stored in the app.config. I work around and cannot do it.
I have in app.config :

<configuration>
  <appSettings>
    <add key="Title" value="EndPoint Supervisor 2"/>
  </appSettings>
</configuration>




我希望在我的标签中使用此绑定:




and I would like this binding in my label :

<Label Grid.Column="1"
                    Name="LabelTitle"
                    Content="{Binding ???}" />



我必须用什么代码替换????与键标题"绑定.
在此先感谢.



With what code must I replace ???? to have a binding with the key "Title".
Thank in advance.

推荐答案

应用程序设置不能直接绑定.但是,您可以引入一个包装应用程序设置的中间模型.

例如:

The application settings cannot be bound directly. You can however introduce an intermidiate model that wraps the application settings.

For example:

public class ConfigurationSettings {
    public string Title { 
        get { return ConfigurationManager.AppSettings["Title"] as String; }
    }
}



然后,您可以将此模型绑定到视图,并使用以下绑定表达式来获取标题.



Then you can bind this model to your view and use the following binding expression to get a hold of the title.

<label text="{Binding Title}" ...="" />


这篇关于来自app.config的WPF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆