How do I store the value of a WPF control as a property parameter in app.config?
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>
a source to share
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))
a source to share