W
W
WayMax2019-04-08 12:18:22
WPF
WayMax, 2019-04-08 12:18:22

How to change the color of WPF elements (take the color from a file)?

I would like to change the colors of WPF elements without recompiling the application.
The color must be stored in a file that can be changed manually.
The options found on the Internet do not work.

options
<Window x:Class="TEST.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TEST"
        xmlns:p="clr-namespace:TEST.Properties"
        Title="MainWindow" Width="800" Height="600">
    <Grid>
        <Label x:Name="label" Content="Test" HorizontalAlignment="Left" Margin="104,101,0,0" VerticalAlignment="Top" Background="{Binding Source={x:Static p:Settings.Default}, Path=SomethingsColor}" Height="107" Width="218"/>
    </Grid>
</Window>

Вариант 1:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key = "SomethingsColor" value = "Black" />
  </appSettings>
</configuration>

Label просто не отображается на форме.
Вариант 2:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
<applicationSettings>
    <TEST.Properties.Settings>
        <setting name="SomethingsColor" serializeAs="String">
            <value>#FF008000</value>
        </setting>
    </TEST.Properties.Settings>
</applicationSettings>
</configuration>

Падает с ошибкой: https://docs.microsoft.com/en-us/previous-versions...

I wanted to change the color to the one that is stored somewhere in the file. The project already has an App.config file so didn't want to create other files. I understand that you can simply open this file in the code, parse it, take colors, fill in the fields with OnPropertyChanged and assign Binding to these fields. But it's a bike. Really there is no ready decision?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Esin, 2019-04-08
@Exomode

You can move your xaml files with style markup or templates to the local directory of the program, and then load them directly at runtime without recompilation, for example, like here

F
Foggy Finder, 2019-04-08
@FoggyFinder

Use dynamic resources. Example:
1. Add to application resources (App.xaml file):

<Application.Resources>
    <SolidColorBrush x:Key="solidGrayBrush" Color="Gray" />

Be careful not to confuse WPF object resources with assembly resources.
2. Refer to the element as a dynamic resource:
<TextBlock
    Background="{DynamicResource solidGrayBrush}"
    Text="Test" />

As an example, we will change the brush to a random one by pressing the button:
Brush[] brushes =
        typeof(Brushes)
        .GetProperties()
        .Select(p => (Brush)p.GetValue(null))
        .ToArray();
Random r = new Random();

and a button click handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
    var b = brushes[r.Next(brushes.Length)];
    Application.Current.Resources["solidGrayBrush"] = b;
}

5cab235c13a81873403570.gif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question