How to access a color in code behind which is defined in a xaml file

N. Hoyer

It try to change a ForeColor or Backcolor with a color which is defined in another seperated xaml file. If i use this:

_toolTip.ForeColor = (Color)Application.Current.Resources["Color_001"];

I will get a exception thrown at the start of the programm: System.InvalidCastException: "Specified cast is not valid."

If i use this:

_toolTip.ForeColor = Resources["Color_001"] as Color;

It tells me that the class Color does not allow Null and cant be referenced with as-operater.

Is there a way i missed out?

Code in ColorBrush.xaml

<Color x:Key="Color_001">#FFFFFFFF</Color>

Code in App.xaml

<Application.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries >
            <ResourceDictionary Source="pack://application:,,,/ColorBrush.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Code in App.xaml.cs

        private void DesignToolTip(string tooltipText)
    {
        _toolTip = new System.Windows.Forms.ToolTip();
        _toolTip.OwnerDraw = true;
        _toolTip.UseAnimation = true;
        _toolTip.Draw += GetToolTipDraw;
        _toolTip.Popup += GetToolTipPopUp;
        _toolTip.ToolTipTitle = tooltipText;
        _toolTip.AutoPopDelay = 500;
        _toolTip.AutomaticDelay = 500;
        _toolTip.ForeColor = (Color)Application.Current.Resources["Color_001"];
        //_toolTip.ForeColor = Resources["Color_001"] as Color;
        //_toolTip.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFFFF");
        _toolTip.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF181818");
    }
Roger Leblanc

Make sure you're casting to the right Color class :

        //Yep, working
        System.Windows.Media.Color color1 = (System.Windows.Media.Color)Application.Current.Resources["Color_001"];

        //Works too
        System.Windows.Media.Color? color2 = Application.Current.Resources["Color_001"] as System.Windows.Media.Color?;

        //InvalidCastException
        System.Drawing.Color color3 = (System.Drawing.Color)Application.Current.Resources["Color_001"];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get access the element in code behind, which is declared inside a DataTemplate in xaml page in Xamarin.Foms?

How to use properties defined in XAML in the C# code-behind

How are XAML elements accessed from code behind in a different XAML file?

Access color string from code behind in .css-File in ASP

How to 'merge' a XAML file and its code-behind with Visual Studio

element defined in xaml "does not exist" in c# code behind

Binding a code-behind defined property to a DataContext property in XAML?

Any way to access to a variable of type "var", defined in code behind, from aspx file?

How to add a new XAML View with code behind

How to bind XAML contentView to its code behind

How to write this XAML Storyboard in Code behind?

How to access a DataContext, that has been specified in XAML, from within code-behind?

How to access the view model instance in a XAML page's code behind (Xamarin Forms) in Prism

UWP WinUI2 xaml How to access element within DataTemplate in code behind

How do I reconnect a XAML view file with it's code behind file?

Custom Color from xaml equivalent in C# code-behind

How to access property defined in code-behind from markup in Web Forms User Control

Access content in XAML from code behind without name

Refactoring property name in code behind does not propagate to XAML file

Generate event handlers in code behind when copying XAML file

How to access textbox control located in DataTemplate tags of <phone:PanoramaItem.HeaderTemplate> from code - behind (MainPage.xaml.cs)

How to access a control templace in code behind?

How to access the selected item in a Droplist in Code Behind?

XamDataGrid - how to access the fields from code behind

How to access selected value from the code behind in drop down list defined inside edititemtemplate using ASP.NET?

How to set a xaml resource value to a constant in code behind

How to change XAML elemen't template from code-behind?

How get x:name of element in code behind / XAML

How to access a variable which is defined in the main thread?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive