tags:

views:

18

answers:

2

We have a WPF windows application that contains a stackpanel control, that I want to be visible only for testing, but not when it is in production.

We'd like to store the visibility value of that stackpanel in the application configuration file (app.config).

What is the WPF way of achieving this?

+1  A: 

First, you create your property in Visual Studio by going to the Project Properties/Settings and creating an Application-scope bool ShowMyStackPanel. This will automatically (1) create a Settings class in the Properties namespace 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 your WPF window, you can now simply bind to Properties.Settings.Default.ShowMyStackPanel using a 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>
Heinzi
small typo with the key of the BooleanToVisibilityConverter.But this was pretty much what I was looking for. Thank you.
Scott Ferguson
Thanks, typo fixed.
Heinzi
+1  A: 

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

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

(assuming the setting is saved as a Visibility value (Visible/Collapsed/Hidden))

Thomas Levesque