[WPF] MVVM 패턴에서의 Combobox 다루기

JEONGKI'S Note·2023년 4월 18일
0

View

<ComboBox ItemsSource="{Binding Source}"
          SelectedItem="{Binding Items}"/>

ItemsSource와 SelectedItem 두 파라메터를 각각 바인딩해준다.

ViewModel

	private Dictionary<string, string>.KeyCollection _Source;
	public Dictionary<string,string>.KeyCollection Source
        {
            get
            {
                return _Source;
            }
            set
            {
                _Source = value;
            }
        }
        private string _Items;
        public string Items
        {
            get
            {
                return _Items;
            }
            set
            {
            	// 선택된 텍스트에 대한 동작추가
                _Items = value;
            }
        }

Dictionary의 KeyCollection를 사용했다. IEnumerable 인터페이스를 구현하거나 IEnumerable 컬렉션으로 변환할 수 있는 다른 데이터 소스를 사용하는
사용자 지정 컬렉션 클래스에 ItemsSource를 바인딩 할 수 있다.

Items 프로퍼티의 set부분에 선택된 텍스트에 따라 동작하게 메소드나 동작을 추가해주면 된다.

profile
주니어 개발자 공부노트입니다 :)

0개의 댓글