Answer the question
In order to leave comments, you need to log in
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.
<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>
<?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>
<?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>
Answer the question
In order to leave comments, you need to log in
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
Use dynamic resources. Example:
1. Add to application resources (App.xaml file):
<Application.Resources>
<SolidColorBrush x:Key="solidGrayBrush" Color="Gray" />
<TextBlock
Background="{DynamicResource solidGrayBrush}"
Text="Test" />
Brush[] brushes =
typeof(Brushes)
.GetProperties()
.Select(p => (Brush)p.GetValue(null))
.ToArray();
Random r = new Random();
private void Button_Click(object sender, RoutedEventArgs e)
{
var b = brushes[r.Next(brushes.Length)];
Application.Current.Resources["solidGrayBrush"] = b;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question