Monday, May 4, 2015

WPF Hex code for colors


The Windows Presentation Foundation (WPF) color names match the Microsoft .NET Framework version 1.0, Windows Forms, and Microsoft Internet Explorer color names. This representation is based on UNIX X11 named color values.

KnownColor enumeration (System.Drawing)
Color Structure (System.Drawing, Color properties)
Colors Class (System.Windows.Media)

Color nameDecimal code
ARGB
Hex code
ARGB
Transparent *0,255,255,255#00FFFFFF
Snow255,255,250,250#FFFFFAFA
GhostWhite255,248,248,255#FFF8F8FF
WhiteSmoke255,245,245,245#FFF5F5F5
Gainsboro255,220,220,220#FFDCDCDC
FloralWhite255,255,250,240#FFFFFAF0
OldLace255,253,245,230#FFFDF5E6
Linen255,250,240,230#FFFAF0E6
AntiqueWhite255,250,235,215#FFFAEBD7
PapayaWhip255,255,239,213#FFFFEFD5
BlanchedAlmond255,255,235,205#FFFFEBCD
Bisque255,255,228,196#FFFFE4C4
PeachPuff255,255,218,185#FFFFDAB9
NavajoWhite255,255,222,173#FFFFDEAD
Moccasin255,255,228,181#FFFFE4B5
Cornsilk255,255,248,220#FFFFF8DC
Ivory255,255,255,240#FFFFFFF0
LemonChiffon255,255,250,205#FFFFFACD
Seashell255,255,245,238#FFFFF5EE
Honeydew255,240,255,240#FFF0FFF0
MintCream255,245,255,250#FFF5FFFA
Azure255,240,255,255#FFF0FFFF
AliceBlue255,240,248,255#FFF0F8FF
Lavender255,230,230,250#FFE6E6FA
LavenderBlush255,255,240,245#FFFFF0F5
MistyRose255,255,228,225#FFFFE4E1
White255,255,255,255#FFFFFFFF
Color nameDecimal code
ARGB
Hex code
ARGB
Black255,0,0,0#FF000000
DarkSlateGray255,47,79,79#FF2F4F4F
DimGray255,105,105,105#FF696969
SlateGray255,112,128,144#FF708090
LightSlateGray255,119,136,153#FF778899
DarkGray255,169,169,169#FFA9A9A9
Gray *255,128,128,128#FF808080
Silver *255,192,192,192#FFC0C0C0
LightGray255,211,211,211#FFD3D3D3
DarkSlateBlue255,72,61,139#FF483D8B
SlateBlue255,106,90,205#FF6A5ACD
MediumSlateBlue255,123,104,238#FF7B68EE
MidnightBlue255,25,25,112#FF191970
Navy255,0,0,128#FF000080
DarkBlue255,0,0,139#FF00008B
MediumBlue255,0,0,205#FF0000CD
Blue255,0,0,255#FF0000FF
RoyalBlue255,65,105,225#FF4169E1
CornflowerBlue255,100,149,237#FF6495ED
DodgerBlue255,30,144,255#FF1E90FF
DeepSkyBlue255,0,191,255#FF00BFFF
SkyBlue255,135,206,235#FF87CEEB
LightSkyBlue255,135,206,250#FF87CEFA
SteelBlue255,70,130,180#FF4682B4
LightSteelBlue255,176,196,222#FFB0C4DE
LightBlue255,173,216,230#FFADD8E6
PowderBlue255,176,224,230#FFB0E0E6
PaleTurquoise255,175,238,238#FFAFEEEE
DarkTurquoise255,0,206,209#FF00CED1
MediumTurquoise255,72,209,204#FF48D1CC
Turquoise255,64,224,208#FF40E0D0
Aqua *255,0,255,255#FF00FFFF
DarkCyan255,0,139,139#FF008B8B
Cyan255,0,255,255#FF00FFFF
LightCyan255,224,255,255#FFE0FFFF
CadetBlue255,95,158,160#FF5F9EA0
MediumAquamarine255,102,205,170#FF66CDAA
Aquamarine255,127,255,212#FF7FFFD4
DarkGreen255,0,100,0#FF006400
DarkOliveGreen255,85,107,47#FF556B2F
DarkSeaGreen255,143,188,143#FF8FBC8F
Teal *255,0,128,128#FF008080
SeaGreen255,46,139,87#FF2E8B57
MediumSeaGreen255,60,179,113#FF3CB371
LightSeaGreen255,32,178,170#FF20B2AA
PaleGreen255,152,251,152#FF98FB98
LightGreen255,144,238,144#FF90EE90
SpringGreen255,0,255,127#FF00FF7F
LawnGreen255,124,252,0#FF7CFC00
Green *255,0,128,0#FF008000
Chartreuse255,127,255,0#FF7FFF00
MediumSpringGreen255,0,250,154#FF00FA9A
GreenYellow255,173,255,47#FFADFF2F
Lime *255,0,255,0#FF00FF00
LimeGreen255,50,205,50#FF32CD32
YellowGreen255,154,205,50#FF9ACD32
ForestGreen255,34,139,34#FF228B22
Olive *255,128,128,0#FF808000
OliveDrab255,107,142,35#FF6B8E23
Color nameDecimal code
ARGB
Hex code
ARGB
DarkKhaki255,189,183,107#FFBDB76B
Khaki255,240,230,140#FFF0E68C
PaleGoldenrod255,238,232,170#FFEEE8AA
LightGoldenrodYellow255,250,250,210#FFFAFAD2
LightYellow255,255,255,224#FFFFFFE0
Yellow255,255,255,0#FFFFFF00
Gold255,255,215,0#FFFFD700
Goldenrod255,218,165,32#FFDAA520
DarkGoldenrod255,184,134,11#FFB8860B
RosyBrown255,188,143,143#FFBC8F8F
IndianRed255,205,92,92#FFCD5C5C
SaddleBrown255,139,69,19#FF8B4513
Sienna255,160,82,45#FFA0522D
Peru255,205,133,63#FFCD853F
Burlywood255,222,184,135#FFDEB887
Beige255,245,245,220#FFF5F5DC
Wheat255,245,222,179#FFF5DEB3
SandyBrown255,244,164,96#FFF4A460
Tan255,210,180,140#FFD2B48C
Chocolate255,210,105,30#FFD2691E
Firebrick255,178,34,34#FFB22222
Brown255,165,42,42#FFA52A2A
DarkSalmon255,233,150,122#FFE9967A
Salmon255,250,128,114#FFFA8072
LightSalmon255,255,160,122#FFFFA07A
Orange255,255,165,0#FFFFA500
DarkOrange255,255,140,0#FFFF8C00
Coral255,255,127,80#FFFF7F50
LightCoral255,240,128,128#FFF08080
Tomato255,255,99,71#FFFF6347
OrangeRed255,255,69,0#FFFF4500
Red255,255,0,0#FFFF0000
HotPink255,255,105,180#FFFF69B4
DeepPink255,255,20,147#FFFF1493
Pink255,255,192,203#FFFFC0CB
LightPink255,255,182,193#FFFFB6C1
PaleVioletRed255,219,112,147#FFDB7093
Maroon255,128,0,0#FF800000
MediumVioletRed255,199,21,133#FFC71585
Magenta255,255,0,255#FFFF00FF
Fuchsia *255,255,0,255#FFFF00FF
Violet255,238,130,238#FFEE82EE
Plum255,221,160,221#FFDDA0DD
Orchid255,218,112,214#FFDA70D6
MediumOrchid255,186,85,211#FFBA55D3
DarkOrchid255,153,50,204#FF9932CC
DarkViolet255,148,0,211#FF9400D3
BlueViolet255,138,43,226#FF8A2BE2
Purple *255,128,0,128#FF800080
MediumPurple255,147,112,219#FF9370DB
Thistle255,216,191,216#FFD8BFD8
DarkMagenta255,139,0,139#FF8B008B
DarkRed255,139,0,0#FF8B0000
Indigo *255,75,0,130#FF4B0082
Crimson *255,220,20,60#FFDC143C
 - - -
 - - -
 - - -
 - - -


Note: This is not my original work. Have blogged it here just for my future reference and of course if someone wants to use it.




Thursday, January 1, 2015

type or namespace name 'Workflow' does not exist

Getting the error "type or namespace name 'Workflow' does not exist in the namespace 'Microsoft.TeamFoundation.Build' #are you missing an assembly reference?#"

When creating workflows I was getting this error. To fix this make sure these 2 things are good:
1) Assembly refereneces - Obvious answer, here is the list of assemblies I had in my project, you can plus or minus as per your requirements

















2) Check the project properties for target framework. Mine was set to .Net framework 4.0 client. i changed it to .Net framework 4.0 and it worked :)



 

Monday, November 17, 2014

Converters - String To Brush Converter

Note: This is not my original work. Have blogged it here just for my future reference and of course if someone wants to use it.

This converter is used to associate a String to a brush color. It was really helpful as we needed to get the color detail from database depending on certain values and apply that color at run time.

 Basic class

public class StringToBrushConverter : IValueConverter
    {
        public StringToBrushConverter()
        {
           
        }

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch (value.ToString())
            {
                case "Gray":
                    return Brushes.Gray;
                case "Red":
                    return Brushes.IndianRed;
                case "Green":
                    return Brushes.LightGreen;
                default:
                    return Brushes.White;
            }
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }


Usage

public StringToBrushConverter StringToBrushConverter { get; set; }
StringToBrushConverter = new StringToBrushConverter();
 var colorBinding = new Binding(visaType + ColorName) { Converter = StringToBrushConverter };
if ( condition1 )
{
stackPanel.SetBinding(StackPanel.BackgroundProperty, colorBinding);
}

Converters - String To Visibility Converter

Note: This is not my original work. Have blogged it here just for my future reference and of course if someone wants to use it.
 
This converter is used to set the visibility property of a control.

Base class

public class StringToVisibilityConverter : IValueConverter
    {
        public Dictionary<string, Visibility> StringVisibilityCollection { get; set; }

        public StringToVisibilityConverter()
        {
            StringVisibilityCollection = new Dictionary<string, Visibility>();
        }

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && StringVisibilityCollection.Keys.Any(s => s == value.ToString()))
            {
                return StringVisibilityCollection[value.ToString()];
            }

            return Visibility.Visible;
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

Usage

public StringToVisibilityConverter StringToVisibilityConverter { get; set; }

StringToVisibilityConverter = new StringToVisibilityConverter();
StringToVisibilityConverter.StringVisibilityCollection.Add("1", Visibility.Collapsed);

//attaching to converter property of control
 var setLink = new FrameworkElementFactory(typeof(Button));
setLink.SetBinding(Button.VisibilityProperty, new Binding(myBinding) { Converter = StringToVisibilityConverter });

Thursday, November 13, 2014

Converters - Bool To Image convertor

Note: This is not my original work. Have blogged it here just for my future reference and of course if someone wants to use it.

This convertor is used to convert a Boolean value into an image. No need to say it is a very useful control provided by WPF.

Base class

public class BoolToImageConverter : IValueConverter
    {
        public BoolToImageConverter()
        {
           
        }

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((value.ToString() == "True" ||  value.ToString() == "1")? true : false)
                return "pack://application:,,,/myApplication
;component/Resources/Set.gif";
            else
                return "pack://application:,,,/
myApplication;component/Resources/UnSet.gif";
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }


Usage

 public BoolToImageConverter boolToImageConverter { get; set; }

boolToImageConverter = new BoolToImageConverter();

var flagImage = new FrameworkElementFactory(typeof(Image));
flagImage.SetBinding(Image.SourceProperty, new Binding(columnName) { Converter = boolToImageConverter });

Converters String To Resource Converter

Note: This is not my original work. Have blogged it here just for my future reference and of course if someone wants to use it.

 Sometime during development you may want to add to your project string to Resource converter to display the message in Cultural specific language. This is a way to create such a converter through code. it is specially helpful when you want to create controls on the fly through code.

Converter class 
 public class StringToResourceConverter : IValueConverter
    {
        public Dictionary<string,string> StringResourceCollection { get; set; }
        public StringToResourceConverter()
        {
            StringResourceCollection = new Dictionary<string, string>();
        }

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && StringResourceCollection.Keys.Any(s => s == value.ToString()))
            {
                return Resources.ResourceManager.GetString(StringResourceCollection[value.ToString()]);
            }

            return string.Empty;
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }


Usage
            ///Declaration
            public StringToResourceConverter StringToResourceConverter { get; set; }
            
            //Initialization
            StringToResourceConverter = new StringToResourceConverter();
            StringToResourceConverter.StringResourceCollection.Add("0", "Set");
            StringToResourceConverter.StringResourceCollection.Add("1", "Remove");


            //attaching to converter property of comtrol
            var setLink = new FrameworkElementFactory(typeof(Button));
            setLink.SetBinding(Button.ContentProperty, new Binding(myString1+ myString2) { Converter = StringToResourceConverter });

Numeric text box

 Note: This is not my original work. Have blogged it here just for my future reference and of course if someone wants to use it.

This post shows how we can create a custom control for our application to allow a common behavior for all textboxes which allow only numeric values. heres the code and the comments make it pretty self explanatory.

public class NumericTextBox :TextBox
   {
       #region Properties
       /// <summary>
       /// Gets or sets the character to be used as decimal separator
       /// </summary>
       public string DecimalSeparator { get; set; }

       /// <summary>
       /// Gets or sets the mask to apply to the textbox
       /// </summary>
       public Boolean IsDecimalAllowed
       {
           get { return (Boolean)GetValue(IsDecimalAllowedProperty); }
           set { SetValue(IsDecimalAllowedProperty, value); }
       }

       /// <summary>
       /// Dependency property to store the decimal is allowed to be entered in the textbox
       /// </summary>
       public static readonly DependencyProperty IsDecimalAllowedProperty =
           DependencyProperty.Register("IsDecimalAllowed", typeof(Boolean), typeof(NumericTextBox), new PropertyMetadata(false));


       /// <summary>
       /// Gets or sets the mask to apply to the textbox
       /// </summary>
       public int Scale
       {
           get { return (int)GetValue(ScaleProperty); }
           set { SetValue(ScaleProperty, value); }
       }

       /// <summary>
       /// Dependency property to store the decimal is allowed to be entered in the textbox
       /// </summary>
       public static readonly DependencyProperty ScaleProperty =
           DependencyProperty.Register("Scale", typeof(int), typeof(NumericTextBox), new PropertyMetadata(0));

       #endregion

       /// <summary>
        /// Static Constructor
        /// </summary>
       static NumericTextBox()
        {   
        }

       /// <summary>
        /// To check the character enetered
        /// </summary>
       protected override void OnPreviewTextInput(TextCompositionEventArgs e)
       {
           e.Handled = !AreAllValidNumericChars(e.Text);
           if (!e.Handled)
           {
               e.Handled = !MaxLengthReached(e);
           }
           base.OnPreviewTextInput(e);
       }

        /// <summary>
        ///To check if numbers entered are all valid numeric numbers
        /// </summary>
        bool AreAllValidNumericChars(string str)
       {
           if (string.IsNullOrEmpty(DecimalSeparator))
               DecimalSeparator = ".";

           bool ret = true;
           if (str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
               str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
               return ret;
           if (IsDecimalAllowed && str == DecimalSeparator)
               return ret;

           int l = str.Length;
           for (int i = 0; i < l; i++)
           {
               char ch = str[i];
               ret &= Char.IsDigit(ch);
           }
          
           return ret;
       }

       /// <summary>
        /// This method was added to prevent arithmetic overflows while saving in db on decimal part.
        /// </summary>
       bool MaxLengthReached(TextCompositionEventArgs e)
       {
           TextBox textBox = (TextBox)e.OriginalSource;
           int precision = textBox.MaxLength - Scale - 2;

           string textToValidate = textBox.Text.Insert(textBox.CaretIndex, e.Text).Replace("-","");
           string[] numericValues = textToValidate.Split(Convert.ToChar(DecimalSeparator));

           if ((numericValues.Length <= 2) && (numericValues[0].Length <= precision) && ((numericValues.Length == 1) || (numericValues[1].Length <= Scale)))
           {
               return true;
           }
           else
           {
               return false;
           }
       }
    }

To use it in XAML is simple like any other default WPF control
 <local:NumericTextBox Width="300" Text="{Binding MyBinding, Mode=TwoWay, UpdateSourceTrigger=LostFocus,   NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ConverterCulture='en-US', StringFormat='F2'}" IsDecimalAllowed="True"
                       MaxLength="20" Scale="2" IsEnabled="false"  />