반응형

1. 프로젝트에 IValueConverter 를 상속받은 InvertableBooleanToVisibilityConverter 추가하여 내부 구현

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class InvertableBooleanToVisibilityConverter : IValueConverter
    {
        private enum Parameters
        {
            Normal, Inverted
        }

        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            var boolValue = (bool)value;
            var direction = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);

            if (direction == Parameters.Inverted)
                return !boolValue ? Visibility.Visible : Visibility.Collapsed;

            return boolValue ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return null;
        }
    }

2. XAML에서 Visibility 속성에 바인딩추가하며 ConvertParameter (Normal / Inverted) 지정

    <UserControl.Resources>
        <Converters:InvertableBooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </UserControl.Resources>


Visibility="{Binding IsChecked,ElementName=checkBoxControl,Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Normal}"
Visibility="{Binding IsChecked,ElementName=checkBoxControl,Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverted}
반응형

+ Recent posts