Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a color in App.xaml and using as a static resource

Tags:

wpf

xaml

EDIT: Looks like this was related to the way I declared the color - using hex value seems to bring it through fine.

EDIT: This about referencing from the App.xaml file. The color itself works fine if declared as a local resource.

I've created a colour:

<SolidColorBrush x:Key="TestBlue">
    <SolidColorBrush.Color>
        <Color R="0" G="86" B="45"/>
    </SolidColorBrush.Color>
</SolidColorBrush>

and placed it in a ResourceDictionary. I've then referenced it in a MergedDictionary:

<Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="Colours.xaml"/>
                <ResourceDictionary  Source="View\Item\ItemResource.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
  </Application.Resources>

When I apply it to the background of a Button as a StaticResource it doesn't apply:

Background="{StaticResource TestBlue}" 

When I Snoop the control it declares the Background as some other value: Background

It also doesn't work if I make it an entry in the itself (like the ViewModelLocator). It does work if I place it in the Window.Resources. Any ideas what's going on here?

like image 210
jidl Avatar asked Jan 01 '26 01:01

jidl


1 Answers

You need to provide value for Alpha (transparency) channel, because Color is structure and it's default value for that value is zero (transparent).

<Color A="255" R="0" G="86" B="45"/>
like image 68
Jan Hamrský Avatar answered Jan 05 '26 23:01

Jan Hamrský



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!