Skip to content

Commit d070f7d

Browse files
committed
Merge pull request #4 from djengineerllc/patch-3
Add converter for show/hide if there is text
2 parents 248e61c + 0f347c4 commit d070f7d

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// --------------------------------------------------------------------
2+
// Obtained from: WPFSmartLibrary
3+
// For more information see : http://wpfsmartlibrary.codeplex.com/
4+
// (by DotNetMastermind)
5+
// --------------------------------------------------------------------
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Globalization;
9+
using System.Linq;
10+
using System.Text;
11+
using System.Windows;
12+
using System.Windows.Data;
13+
using System.Windows.Markup;
14+
15+
namespace MahApps.Metro.Converters
16+
{
17+
/// <summary>
18+
/// Converts a String into a Visibility enumeration (and back)
19+
/// The FalseEquivalent can be declared with the "FalseEquivalent" property
20+
/// </summary>
21+
[ValueConversion(typeof(string), typeof(Visibility))]
22+
[MarkupExtensionReturnType(typeof(StringToVisibilityConverter))]
23+
public class StringToVisibilityConverter : MarkupExtension, IValueConverter
24+
{
25+
/// <summary>
26+
/// FalseEquivalent (default : Visibility.Collapsed => see Constructor)
27+
/// </summary>
28+
public Visibility FalseEquivalent { get; set; }
29+
30+
/// <summary>
31+
/// Define whether the opposite boolean value is crucial (default : false)
32+
/// </summary>
33+
public bool OppositeStringValue { get; set; }
34+
35+
/// <summary>
36+
/// Initialize the properties with standard values
37+
/// </summary>
38+
public StringToVisibilityConverter()
39+
{
40+
this.FalseEquivalent = Visibility.Collapsed;
41+
this.OppositeStringValue = false;
42+
}
43+
44+
#region IValueConverter Members
45+
46+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
47+
{
48+
if (value is string && targetType == typeof(Visibility))
49+
{
50+
if (this.OppositeStringValue == true)
51+
{
52+
return ((value as string).ToLower().Equals(String.Empty)) ? Visibility.Visible : this.FalseEquivalent;
53+
}
54+
return ((value as string).ToLower().Equals(String.Empty)) ? this.FalseEquivalent : Visibility.Visible;
55+
}
56+
return value;
57+
}
58+
59+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
60+
{
61+
if (value is Visibility)
62+
{
63+
if (this.OppositeStringValue == true)
64+
{
65+
return ((Visibility)value == Visibility.Visible) ? String.Empty : "visible";
66+
}
67+
return ((Visibility)value == Visibility.Visible) ? "visible" : String.Empty;
68+
}
69+
return value;
70+
}
71+
72+
#endregion
73+
74+
#region MarkupExtension "overrides"
75+
76+
public override object ProvideValue(IServiceProvider serviceProvider)
77+
{
78+
return new StringToVisibilityConverter
79+
{
80+
FalseEquivalent = this.FalseEquivalent,
81+
OppositeStringValue = this.OppositeStringValue
82+
};
83+
}
84+
85+
#endregion
86+
}
87+
}

0 commit comments

Comments
 (0)