Return to Snippet

Revision: 25010
at March 17, 2010 17:39 by davidsi


Initial Code
/** 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);
        }
    } 
}

Initial URL

                                

Initial Description
Value converters allow ease of converting code-behind values to dependency property values

Initial Title
Value Converters for WPF and Silverlight

Initial Tags
c

Initial Language
C#