your programing

WPF-콤보 상자에 정적 항목 추가

lovepro 2020. 10. 7. 07:59
반응형

WPF-콤보 상자에 정적 항목 추가


나는 전에 그것을 말했고 나는 그것을 다시 말할 것이다. WPF의 가장 쉬운 예는 또한 웹에서 찾기가 가장 어렵다. :)

표시해야하는 콤보 상자가 있지만 데이터 바인딩 또는 다른 항목 일 필요는 없으며 콘텐츠는 정적입니다. XAML을 사용하여 콤보 상자에 정적 항목 목록을 추가하려면 어떻게해야합니까?


다음은 MSDN의 코드와 자세한 내용을 확인해야하는 기사 링크 입니다.

<ComboBox Text="Is not open">
    <ComboBoxItem Name="cbi1">Item1</ComboBoxItem>
    <ComboBoxItem Name="cbi2">Item2</ComboBoxItem>
    <ComboBoxItem Name="cbi3">Item3</ComboBoxItem>
</ComboBox>

이렇게 :

<ComboBox Text="MyCombo">
<ComboBoxItem  Name="cbi1">Item1</ComboBoxItem>
<ComboBoxItem  Name="cbi2">Item2</ComboBoxItem>
<ComboBoxItem  Name="cbi3">Item3</ComboBoxItem>
</ComboBox>

코드에 항목을 추가 할 수도 있습니다.

cboWhatever.Items.Add("SomeItem");

또한 디스플레이 / 값을 제어하는 ​​곳에 무언가를 추가하려면 (제 경험상 거의 절대적으로 필요함) 그렇게 할 수 있습니다. 여기서 좋은 stackoverflow 참조를 찾았습니다.

WPF의 키 값 쌍 콤보 상자

요약 코드는 다음과 같습니다.

ComboBox cboSomething = new ComboBox();
cboSomething.DisplayMemberPath = "Key";
cboSomething.SelectedValuePath = "Value";
cboSomething.Items.Add(new KeyValuePair<string, string>("Something", "WhyNot"));
cboSomething.Items.Add(new KeyValuePair<string, string>("Deus", "Why"));
cboSomething.Items.Add(new KeyValuePair<string, string>("Flirptidee", "Stuff"));
cboSomething.Items.Add(new KeyValuePair<string, string>("Fernum", "Blictor"));

<ComboBox Text="Something">
            <ComboBoxItem Content="Item1"></ComboBoxItem >
            <ComboBoxItem Content="Item2"></ComboBoxItem >
            <ComboBoxItem Content="Item3"></ComboBoxItem >
</ComboBox>

참고 URL : https://stackoverflow.com/questions/1791784/wpf-add-static-items-to-a-combo-box

반응형