1: public class BoolToVisibilityConverter: IValueConverter
2: {
3:
4: #region IValueConverter Members
5:
6: /// <summary>
7: /// Convert method from bool to visibility
8: /// </summary>
9: /// <param name="value">the boolean/visibility value value</param>
10: /// <param name="targetType"></param>
11: /// <param name="parameter">mappingmode = pro or contra. Pro will map true to visible, contra
12: /// will map true to collapsed</param>
13: /// <param name="culture"></param>
14: /// <returns></returns>
15: public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
16: {
17: bool normalDirection = true;
18:
19: if (parameter != null)
20: {
21: if (parameter.ToString().Trim().ToLower() == "contra")
22: normalDirection = false;
23: }
24:
25: if (value is bool)
26: {
27: if ((bool)value)
28: {
29: return normalDirection ? Visibility.Visible : Visibility.Collapsed;
30: }
31: else
32: {
33: return normalDirection ? Visibility.Collapsed : Visibility.Visible;
34: }
35: }
36: else
37: {
38: return Visibility.Visible;
39: }
40: }
41:
42: public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
43: {
44: bool normalDirection = true;
45:
46: if (parameter.ToString().Trim().ToLower() == "contra")
47: normalDirection = false;
48:
49: if (value is Visibility)
50: {
51: if ((Visibility)value == Visibility.Visible)
52: {
53: return normalDirection ? true : false;
54: }
55: else
56: {
57: return normalDirection ? false : true;
58: }
59: }
60: else
61: {
62: return true;
63: }
64: }
65:
66: #endregion
67: }