Showing posts with label Converters. Show all posts
Showing posts with label Converters. Show all posts

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 });