简体   繁体   中英

Xaml reference static properties like System.Windows.Media.Colors

I'm defining a custom palette class in a similar way to the inbuilt System.Windows.Media.Colors static class.

Ideally I would like to reference this in a similar way to the Colors class. eg

<Rectangle Fill="Red"/>

The most concise version I have so far is:

<Rectangle Fill="{x:Static Palette:PrimaryPalette.Red}"/>

Also my PrimaryPalette class has properties with a return type SolidColorBrush rather than Color.

So my question is two fold

  1. Is there a more concise method of referencing a custom color set?
  2. Is the inbuilt Colors class special within the framework or is there some way of creating a class which operates in similar fashion?

If verbosity and ease-of-use is your primary concern then I would recommend using a custom MarkupExtension:

public class CustomColor : MarkupExtension
{
    [ConstructorArgument("Key")]
    public object Key { get; set; }

    public CustomColor()
    {
    }

    public CustomColor(object key)
    {
        this.Key = key;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        try
        {
            var color = (Color)ColorConverter.ConvertFromString(this.Key.ToString()); 
            return new SolidColorBrush(color);
        }
        catch
        {
            return new SolidColorBrush(Colors.Transparent);
        }
    }

}

By following the tips on this page you can also add the namespace for this extension to the XAML processor which in turn allows you to use it like this:

<Rectangle Fill="{CustomColor Blue}"  />

This is just one way of doing things, in this particular example I'm simply parsing the key string and converting it to a color, but you can replace that with any key type and/or conversion code you want.

Marks answer is the correct answer I just thought I would share my usage which is slightly different than Marks but only due to my specific needs.

In my case I wanted a set of new colours to be able to be selected by name so to achieve this I utilised a dictionary

public class PrimaryPalette : MarkupExtension
{
    public static Dictionary<PrimaryPaletteColours, Color> Palette => 
        new Dictionary<PrimaryPaletteColours, Color>
        {
            { PrimaryPaletteColours.CustomMagenta,    Color.FromArgb(0xFF, 0xDA, 0x42, 0xAA) },
            { PrimaryPaletteColours.CustomBlue,       Color.FromArgb(0xFF, 0x11, 0x42, 0xFF) },
            { PrimaryPaletteColours.CustomGreen,      Color.FromArgb(0xFF, 0x33, 0xDE, 0x60) },
            { PrimaryPaletteColours.CustomOrange,     Color.FromArgb(0xFF, 0xDA, 0x80, 0x22) },
            { PrimaryPaletteColours.CustomPurple,     Color.FromArgb(0xFF, 0xCC, 0x00, 0xFF) },
            { PrimaryPaletteColours.CustomRed,        Color.FromArgb(0xFF, 0xEE, 0x42, 0x00) },
            { PrimaryPaletteColours.CustomTurqoise,   Color.FromArgb(0xFF, 0x10, 0xAB, 0xBC) },
            { PrimaryPaletteColours.CustomGold,       Color.FromArgb(0xFF, 0xDA, 0xA0, 0x22) }
        };

    public PrimaryPalette() { }

    public PrimaryPalette(PrimaryPaletteColours key) { Key = key; }

    [ConstructorArgument("Key")]
    public PrimaryPaletteColours Key { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        try
        {
            return new SolidColorBrush(Palette[Key]);
        }
        catch
        {
            return new SolidColorBrush(Colors.Transparent);
        }
    }
}

public enum PrimaryPaletteColours
{
    CustomMagenta,
    CustomBlue,
    CustomGreen,
    CustomOrange,
    CustomPurple,
    CustomRed,
    CustomTurqoise,
    CustomGold
}

After the namespace tweak Mark suggested my usage is:

{PrimaryPalette CustomMagenta}

This method also gives you intellisense and compile time verification to ensure the color name is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM