본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - ComboBox & Enum 사용법 ( 콤보박스, 드롭박스, DropBox )

728x90
반응형
 WPF & C# - ComboBox & Enum 사용법 ( 콤보박스, 드롭박스, DropBox )

testComBoBox.zip
0.04MB
testComBoBox.exe
0.01MB

 

 

MainWindow.xaml

<Grid>
<StackPanel>
    <ComboBox x:Name="cbx01" SelectionChanged="cbx01_SelectionChanged"></ComboBox>
    <Label x:Name="lbl"></Label>
    <Label x:Name="lbl2"></Label>
</StackPanel>
</Grid>

 

MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent();

    // 입력방식 : enum 명칭 ( Naver, Kakao, Google )
    cbx01.ItemsSource = Enum.GetValues(typeof(enumServer));
}

enum enumServer { Naver = 1, Kakao, Google = 4 }
enumServer selectServer;

private void cbx01_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(selectServer == enumServer.Naver)
    {
    	MessageBox.Show("Naver이 선택되었습니다.")
    }

	lbl.Content = (int)cbx01.SelectedValue + " : "+ (enumServer)cbx01.SelectedValue;

    selectServer = (enumServer)cbx01.SelectedValue;
    lbl2.Content = (int)selectServer + " : " + selectServer;
}

 

- Enum에 입력된 값을 ItemsSource를 통하여 한번에 ComboBox 로 밀어 넣는다.

- ComboBox 에서 선택된 값을 enum 값과 enum 명칭으로 구분하여 출력한다.

ComboBox 에서는 가장 많이 쓰일 기능이라 가장 간단하게 정리해보았다.

 

728x90
반응형