【问题标题】:How do you store a WPF control value as a Property Setting in app.config?如何将 WPF 控件值存储为 app.config 中的属性设置?
【发布时间】:2011-02-19 07:41:19
【问题描述】:

我们有一个 WPF windows 应用程序,其中包含一个 stackpanel 控件,我希望它仅在测试时可见,而不是在生产时可见。

我们想将该堆栈面板的可见性值存储在应用程序配置文件 (app.config) 中。

实现这一点的 WPF 方法是什么?

【问题讨论】:

    标签: wpf app-config


    【解决方案1】:

    首先,通过转到项目属性/设置并创建应用程序范围布尔 ShowMyStackPanel,在 Visual Studio 中创建您的属性。这将自动 (1) 在 Properties 命名空间中创建一个 Settings 类,并 (2) 将以下内容添加到您的 app.config:

    <configuration>
        ...
        <applicationSettings>
            <CsWpfApplication1.Properties.Settings>
                <setting name="ShowMyStackPanel" serializeAs="String">
                    <value>False</value>
                </setting>
            </CsWpfApplication1.Properties.Settings>
        </applicationSettings>
    </configuration>
    

    在您的 WPF 窗口中,您现在可以使用 BooleanToVisibilityConverter 简单地绑定到 Properties.Settings.Default.ShowMyStackPanel

    <Window ...
        xmlns:prop="clr-namespace:CsWpfApplication1.Properties"
        ...>
        <Window.Resources>
            <BooleanToVisibilityConverter x:Key="MyBoolToVisibilityConverter" />
        </Window.Resources>
        ...
            <StackPanel Visibility="{Binding Source={x:Static prop:Settings.Default},
                                             Path=ShowMyStackPanel,
                                             Converter={StaticResource MyBoolToVisibilityConverter}}">
                ...
            </StackPanel>
        ...
    </Window>
    

    【讨论】:

    • BooleanToVisibilityConverter 键的小错字。但这几乎就是我想要的。谢谢。
    【解决方案2】:

    您可以使用following markup extension 绑定到设置:

    <StackPanel Visibility="{my:SettingBinding StackPanelVisibility}">
    ...
    

    (假设设置保存为Visibility 值(可见/折叠/隐藏))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 2011-06-28
      • 2011-07-22
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 2018-07-27
      相关资源
      最近更新 更多