-1

I'm developing an application in VB.net with WPF and I would like the end-user to be able to change the colors of the application from the settings page.

This is a snippet of the XAML I have for one of the vector paths: enter image description here

Bear in mind, all my icons are vectors so there are around 4410 lines of XAML. My thinking is to set the HEX code to a variable and in the VB code, I can set the variable to something like "FFF15329":

enter image description here

Is something like this possible? I am more than happy to go through and set the existing HEX codes to variables or something else.

Nathan
  • 65
  • 1
  • 4
  • 14
  • And this XAML is for... WPF? Silverlight? UWP? Xamarin? MAUI? XAML is XML, so you could always read it into an XDocument using LINQ to XML... – Heretic Monkey Oct 06 '21 at 19:10
  • Sorry, I am using WPF, I have edited the question to include this :) – Nathan Oct 06 '21 at 19:11
  • I don't think you should store that color in the XAML, rather in a settings file which will be read at runtime – djv Oct 06 '21 at 19:13
  • How would the XAML read from the settings file? Also, would the VB code be able to modify the settings files? – Nathan Oct 06 '21 at 19:15
  • There is a wealth of information on Google: *wpf change control color at runtime* – djv Oct 06 '21 at 19:24
  • 2
    Does this answer your question? [WPF: Changing Resources (colors) from the App.xaml during runtime](https://stackoverflow.com/questions/786183/wpf-changing-resources-colors-from-the-app-xaml-during-runtime) – Mak Ahmed Oct 06 '21 at 20:53
  • @MakAhmed I just tested that, I can set the color in XAML to "BackgroundColor" which is yellow, But I try to set the color with this: Application.Current.Resources("BackgroundColor") = "Blue", which doesn't work. Any ideas? – Nathan Oct 06 '21 at 21:10

1 Answers1

0

Unable to use your code because you had given it as image,, but it will be like below example, replace your Path Fill Color with Dependency-Property and then set this Dependency-Property from your Main Window

Custom Control -- HeaderButton

<Grid>
    <Border CornerRadius="6" BorderThickness="2" 
            Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HeaderButton},Mode=FindAncestor},Path=BackgroundColor}">
    </Border>
</Grid>

Code Behind

public static readonly DependencyProperty BackgroundColorProperty = 
DependencyProperty.Register(nameof(BackgroundColor),typeof(string),typeof(HeaderButton),new PropertyMetadata("Transparent"));

    public string BackgroundColor
    {
        get => (string)GetValue(BackgroundColorProperty);
        set => SetValue(BackgroundColorProperty,value);
    }

Mainindow.xaml

<local:HeaderButton Width="100" Height="100" BackgroundColor="Red" />
Mak Ahmed
  • 578
  • 5
  • 16