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 });
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 });
No comments:
Post a Comment