wpf combobox selected value 가져오는방법?

안되는 상황을 좀 더 정확히 설명해줄 수 있을까요?

(참고로 ViewModel에 View의 종속성이 생겨서는 안됩니다. Visibility의 경우 뷰에 종속성이 있으므로 컨버터를 구현하거나 저처럼 트리거를 이용하는 방법이 있습니다.)


다음의 코드로 동작을 확인해 볼 수 있습니다.

| MainWindow.xaml

<Window
    x:Class="WpfApp31.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp31"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <StackPanel Orientation="Vertical">
        <ComboBox ItemsSource="{Binding Enums}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
        <Border
            Width="150"
            Height="150"
            Background="Green">
            <Border.Style>
                <Style TargetType="Border">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsShow}" Value="False">
                            <Setter Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
        </Border>
        <TextBlock Text="{Binding SelectedItem}" HorizontalAlignment="Center"/>
    </StackPanel>
</Window>

| MainWindow.xaml.cs (테스트를 위해 ViewModel 없이 View에서 INotifyPropertyChanged를 구현)

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;


namespace WpfApp31
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        private string _selectedItem = string.Empty;
        private bool _isShow = false;

        public string SelectedItem
        {
            get => _selectedItem;
            set
            {
                if (_selectedItem == value)
                    return;

                _selectedItem = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));

                if (SelectedItem is "NV")
                    IsShow = false;
                else
                    IsShow = true;
            }
        }

        public bool IsShow
        {
            get => _isShow;
            set
            {
                if (_isShow == value)
                    return;

                _isShow = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsShow)));
            }
        }

        public IEnumerable<string> Enums { get; } = new[] { "NV", "1", "2", "3 " };

        public MainWindow()
        {
            DataContext = this;

            InitializeComponent();

            SelectedItem = "NV";
        }

    }
}

| 동작 화면
: SelectedItem이 NV일 경우,
image

:이외의 값일 경우
image

image

image

2개의 좋아요