WPF - add static items to a combo box

前端 未结 4 754
旧巷少年郎
旧巷少年郎 2020-12-29 00:31

I\'ve said it before and I\'ll say it again, the easiest examples for WPF are also the hardest to find on the web :)

I have a combo box that I need to display but it

相关标签:
4条回答
  • 2020-12-29 01:23

    Here is the code from MSDN and the link - Article Link, which you should check out for more detail.

    <ComboBox Text="Is not open">
        <ComboBoxItem Name="cbi1">Item1</ComboBoxItem>
        <ComboBoxItem Name="cbi2">Item2</ComboBoxItem>
        <ComboBoxItem Name="cbi3">Item3</ComboBoxItem>
    </ComboBox>
    
    0 讨论(0)
  • 2020-12-29 01:24
    <ComboBox Text="Something">
                <ComboBoxItem Content="Item1"></ComboBoxItem >
                <ComboBoxItem Content="Item2"></ComboBoxItem >
                <ComboBoxItem Content="Item3"></ComboBoxItem >
    </ComboBox>
    
    0 讨论(0)
  • 2020-12-29 01:27

    Like this:

    <ComboBox Text="MyCombo">
    <ComboBoxItem  Name="cbi1">Item1</ComboBoxItem>
    <ComboBoxItem  Name="cbi2">Item2</ComboBoxItem>
    <ComboBoxItem  Name="cbi3">Item3</ComboBoxItem>
    </ComboBox>
    
    0 讨论(0)
  • 2020-12-29 01:33

    You can also add items in code:

    cboWhatever.Items.Add("SomeItem");
    

    Also, to add something where you control display/value, (almost categorically needed in my experience) you can do so. I found a good stackoverflow reference here:

    Key Value Pair Combobox in WPF

    Sum-up code would be something like this:

    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"));
    
    0 讨论(0)
提交回复
热议问题