更改文本块前景

时间:2014-05-08 10:07:20

标签: c# windows-phone-8

我有像这样#dc020d的RGB颜色,我有一个文本块,我想让文本块的前景等于我作为字符串的RGB颜色。

我试过

var bc = new BrushConverter();

tb.Foreground = (Brush)bc.ConvertFrom("#FFXXXXXX");

但它没有用,虽然我使用的是System.Windows.Media

我找到Color.FromArgb()的唯一方法,但需要四个字节,我无法将字符串转换为四个字节。

3 个答案:

答案 0 :(得分:7)

创建一个采用十六进制颜色字符串并返回SolidColorBrush的方法。

像:

public SolidColorBrush GetBrushFromHexColor(string hexColor)
{
    return new SolidColorBrush(
        Color.FromArgb(
            Convert.ToByte(hexColor.Substring(1, 2), 16),
            Convert.ToByte(hexColor.Substring(3, 2), 16),
            Convert.ToByte(hexColor.Substring(5, 2), 16),
            Convert.ToByte(hexColor.Substring(7, 2), 16)
        )
    );
}

并将此函数调用到您将颜色设置为文本块的行。

MyTxt.Foreground = GetBrushFromHexColor("#ffdc020d");

答案 1 :(得分:0)

您可以使用:

// the same with #94b301
Color myColor = Color.FromArgb(255, 0x94, 0xb3, 0x01);

答案 2 :(得分:0)

试试这个。

// Description : Extension methods for using applying solid color in WinRT XAML projects.
// Owner : Farhan Ghumra
// Contact : https://www.facebook.com/farhan.ghumra
//           https://twitter.com/_F4RH4N_
// Example :
// MyRectagle.Fill = ColorExtensions.FromName("SkyBlue");
// MyTextBlock.Foreground = ColorExtensions.FromHex("#379988");

using Windows.UI;
using Windows.UI.Xaml.Media;
using System.Reflection;

namespace App1
{
    public static class ColorExtensions
    {
        /// <summary>
        /// It gives solid color brush from hexadecimal color code.
        /// </summary>
        /// <param name="HexColor">Color code in hexadecimal format (Example : #123456 or #12345678)</param>
        /// <returns>New instance of SolidColorBrush accoiding to hexadecimal color.</returns>
        public static SolidColorBrush FromHex(string HexColor)
        {
            byte A, R, G, B;

            if (HexColor.Length == 7)
            {
                A = 255;
                R = HexColor.Substring(1, 2).ToByte();
                G = HexColor.Substring(3, 2).ToByte();
                B = HexColor.Substring(5, 2).ToByte();
                return new SolidColorBrush(Color.FromArgb(A, R, G, B));
            }

            else if (HexColor.Length == 9)
            {
                A = HexColor.Substring(1, 2).ToByte();
                R = HexColor.Substring(3, 2).ToByte();
                G = HexColor.Substring(5, 2).ToByte();
                B = HexColor.Substring(7, 2).ToByte();
                return new SolidColorBrush(Color.FromArgb(A, R, G, B));
            }

            else
            {
                return new SolidColorBrush(Colors.Transparent);
            }
        }

        /// <summary>
        /// It gives solid color brush from color name.
        /// </summary>
        /// <param name="name">Color name as string.</param>
        /// <returns>New instance of SolidColorBrush according to color name.</returns>
        public static SolidColorBrush FromName(string ColorName)
        {
            var ColorProperty = typeof(Colors).GetRuntimeProperty(ColorName);

            if (ColorProperty != null)
                return new SolidColorBrush((Color)ColorProperty.GetValue(null));
            else
                return new SolidColorBrush(Colors.Transparent);
        }

        /// <summary>
        /// Converts hexadecimal color code channel (Alpha, Red, Blue, Green) to byte type.
        /// </summary>
        /// <param name="ColorCodePart">Hexadecimal color code channel (Alpha, Red, Blue, Green)</param>
        /// <returns>byte value parsed from Hexadecimal color code channel</returns>
        private static byte ToByte(this string ColorCodePart)
        {
            return byte.Parse(ColorCodePart, System.Globalization.NumberStyles.HexNumber);
        }
    }
}