Search code examples
wpf

How can I set value of style with Configuration File?


I want set property for all the label of my solution . so i write style in app.xaml but i want create the possibility for users that they can change value of its style. help me to set the value with configuration file value ← configurationmanager.appsetting.get(" ... ").


Solution

  • Instead of using ConfigurationManager you could bind property values in a style to application settings. Assume an application setting ButtonBackground of type SolidColorBrush, the app.config would contain this:

    <applicationSettings>
        <TestApp.Properties.Settings>
            <setting name="ButtonBackground" serializeAs="String">
                <value>#FF008000</value>
            </setting>
        </TestApp.Properties.Settings>
    </applicationSettings>
    

    In App.xaml you could bind a style property to an application setting like this:

    <Application x:Class="TestApp.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:p="clr-namespace:TestApp.Properties"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <Style TargetType="Button">
                <Setter Property="Background" Value="{Binding Source={x:Static p:Settings.Default}, Path=ButtonBackground}"/>
            </Style>
        </Application.Resources>
    </Application>