18
|
1 // http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/
|
|
2 // to use in xaml: <TextBlock Text="{Binding SomePath, Converter={src:BoolVisibilityConverter}}" />
|
|
3
|
|
4 using System;
|
|
5 using System.Windows;
|
|
6 using System.Windows.Data;
|
|
7 using System.Windows.Markup;
|
|
8 using System.Globalization;
|
|
9
|
|
10 namespace MetroWpf.Xaml.Converters
|
|
11 {
|
|
12 public class BooleanToVisibilityConverter : MarkupExtension, IValueConverter
|
|
13 {
|
|
14 private static BooleanToVisibilityConverter booleanToVisibilityConverter = null;
|
|
15
|
|
16 public override object ProvideValue(IServiceProvider serviceProvider)
|
|
17 {
|
|
18 if (null == booleanToVisibilityConverter)
|
|
19 booleanToVisibilityConverter = new BooleanToVisibilityConverter();
|
|
20
|
|
21 return booleanToVisibilityConverter;
|
|
22 }
|
|
23
|
|
24 public bool IsReversed { get; set; }
|
|
25 public bool UseHidden { get; set; }
|
|
26
|
|
27 #region IValueConverter Members
|
|
28
|
|
29 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
30 {
|
|
31 var val = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
|
|
32 if (this.IsReversed)
|
|
33 {
|
|
34 val = !val;
|
|
35 }
|
|
36 if (val)
|
|
37 {
|
|
38 return Visibility.Visible;
|
|
39 }
|
|
40 return this.UseHidden ? Visibility.Hidden : Visibility.Collapsed;
|
|
41 }
|
|
42 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
43 {
|
|
44 throw new NotImplementedException();
|
|
45 }
|
|
46
|
|
47 #endregion
|
|
48 }
|
|
49 }
|