How do I store the value of a WPF control as a property parameter in app.config?

We have a Windows WPF application that contains a stack control that I only want to see for testing, but not when it is in production.

We want to store the visibility value of this stack bar in the application config file (app.config).

What is the way to achieve this WPF?

+2


a source to share


2 answers


First, you will create your property in Visual Studio by going to Project Properties / Settings and creating a bool application area ShowMyStackPanel

. This will automatically (1) create a class Settings

in the namespace Properties

and (2) add the following to your app.config:

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

      



In a WPF window, you can now simply bind to Properties.Settings.Default.ShowMyStackPanel

with BooleanToVisibilityConverter

:

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

      

+1


a source


You can use the following markup extension to bind to the setting:

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

      



(if the parameter is stored as value Visibility

(visible / folded / hidden))

+1


a source







All Articles