본문 바로가기
C#/WPF

[WPF] Converter와 Visibility

by HJ0216 2024. 3. 18.

최근에 프로젝트를 진행하면서 ListView Control을 하며 어려웠던 부분을 정리하였습니다.

 

👉 기본 환경

- Language: C#, xaml

- IDE: Visual Basic 2022


bool 타입의 값으로 Visibility를 컨트롤 하는 일이 많았습니다.

 

이번 기회에 사용한 Converter를 정리🗂️해 봅니다.

 

BooleanToVisibleConverter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class BooleanToVisibleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool val = (bool)value;
        if (val)
        {
            return System.Windows.Visibility.Visible;
        }
        else
        {
            return System.Windows.Visibility.Collapsed;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

NegativeBooleanConverter

1
2
3
4
5
6
7
8
9
10
11
public class NegativeBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return !((bool)value);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

ChainConverter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ChainConverter : IValueConverter
{
    public IValueConverter Converter1 { get; set; }
    public IValueConverter Converter2 { get; set; }
 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        object convertedValue = Converter1.Convert(value, targetType, parameter, culture);
        return Converter2.Convert(convertedValue, targetType, parameter, culture);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

Window / UserControl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<Window>
    <Window.Resources>
        <ResourceDictionary>
            <converter:BooleanToVisibleConveter x:Key="boolVisibleInverter"/>
            <converter:NegativeBooleanConverter x:Key="negativeBoolInverter"/>
            <converter:ChainConverter x:Key="negativeBoolVisibleInverter"
                                      Converter1="{StaticResource negativeBoolInverter}"
                                      Converter2="{StaticResource boolVisibleInverter}" />
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBlock Text="If the value is true, this TextBlock will be Visible"
                       Visibility="{Binding TrueValue, Converter={StaticResource boolVisibleInverter}}" />
            <TextBlock Text="If the value is false, this TextBlock will be Visible"
                       Visibility="{Binding FalseValue, Converter={StaticResource negativeBoolVisibleInverter}}" />
        </StackPanel>
        <Grid Background="#000000" Opacity="0.5" 
              Visibility="{Binding TrueValue, Converter={StaticResource boolVisibleInverter}}">
        </Grid>
    </Grid>
</Window>

 

⭐ Converter와 Visibility를 통해서 Dim 처리를 구현할 수 있음

    * Dim 처리: 웹페이지에서 팝업을 띄울때 배경으로부터 팝업화면을 강조하기 위해 넣는 반투명 음영 영역

    (여기서는 18행의 Grid가 Dim 역할을 수행)

 

 

 

📚 참고 자료

 

WPF: how to use 2 converters in 1 binding?

I have a control that I want to show/hide, depending on the value of a boolean. I have a NegatedBooleanConverter (switches true to false and vice versa) and I need to run this converter first. I h...

stackoverflow.com

 

'C# > WPF' 카테고리의 다른 글

[WPF] MVVM Scroll 동작 구현  (0) 2024.03.31
[WPF] ListView와 SelectedItem 초기화  (0) 2024.03.31
[WPF] ResourceDictionary  (0) 2024.03.18
[WPF] Popup Control  (1) 2024.03.17
[WPF] 검색과 정렬  (0) 2024.03.10