Last active
July 11, 2018 14:18
-
-
Save mniak/bac6bfc08ae3a5fd0caf to your computer and use it in GitHub Desktop.
#wpf converters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using System.Windows.Data; | |
namespace WpfConverters | |
{ | |
public class BooleanToVisibilityConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
if (!(value is bool)) | |
return DependencyProperty.UnsetValue; | |
var result = (bool)value | |
? Visibility.Visible | |
: Visibility.Collapsed; | |
return result; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
if (!(value is Visibility)) | |
return DependencyProperty.UnsetValue; | |
var v = (Visibility)value; | |
var result = v == Visibility.Visible | |
? true | |
: false; | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Globalization; | |
using System.Linq; | |
using System.Windows; | |
using System.Windows.Data; | |
namespace WpfConverter | |
{ | |
public class CountConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
var val = value as IEnumerable; | |
if (val == null) | |
return DependencyProperty.UnsetValue; | |
var count = val.Cast<object>().Count(); | |
return count; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotSupportedException(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Windows; | |
using System.Windows.Data; | |
namespace WpfConverters | |
{ | |
public class EnumToBooleanConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
string parameterString = parameter as string; | |
if (parameterString == null) | |
return DependencyProperty.UnsetValue; | |
if (Enum.IsDefined(value.GetType(), value) == false) | |
return DependencyProperty.UnsetValue; | |
object parameterValue = Enum.Parse(value.GetType(), parameterString); | |
return parameterValue.Equals(value); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
string parameterString = parameter as string; | |
if (parameterString == null) | |
return DependencyProperty.UnsetValue; | |
return Enum.Parse(targetType, parameterString); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Windows; | |
using System.Windows.Data; | |
namespace WpfConverters | |
{ | |
public class NegateBooleanConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
if (!(value is bool)) | |
return DependencyProperty.UnsetValue; | |
var result = !(bool)value; | |
return result; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return Convert(value, targetType, parameter, culture); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment