/ Published in: C#
Value converters allow ease of converting code-behind values to dependency property values
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** XAML code <src:VisibilityFromBoolConverter x:Key="DefaultVisibleConverter" /> <!-- src is the app namespace, declared in header --> <Border Visibility="{Binding Selected, Converter={StaticResource DefaultVisibleConverter}}" Background="White" > <!-- usage of converter --> **/ using System; using System.Globalization; // CultureInfo using System.Windows.Data; // IValueConverter namespace mypcforsilvernet { public class VisibilityFromBoolConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return Visibility.Visible; bool visibility = (bool)value; return visibility ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Visibility visibility = (Visibility)value; return (visibility == Visibility.Visible); } } }