annotate MetroWpf/MetroWpf.Xaml/Converters/BooleanToVisibilityConverter.cs @ 121:8f94475d3146 tip

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