반응형
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}
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
C# Tray Icon 만들기 (0) | 2021.10.27 |
---|---|
간편한 INI 파일 Handler (0) | 2021.10.26 |
[WPF] Pack URI (0) | 2021.10.26 |
WPF Visibility 속성을 Radiobutton 의 IsChedched 속성과 바인딩하기 (0) | 2021.10.26 |
.Net Core Entity Framework Db First Sqlite (0) | 2021.10.24 |