是否可以在wpf中的两个项目之间添加组合框项目?

时间:2012-06-08 15:54:31

标签: c# wpf

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Trainning.Mycombobox"
x:Name="Window"
Title="Mycombobox"
Width="640" Height="480">

<Grid x:Name="LayoutRoot">
    <ComboBox x:Name="com_friends" Height="32" Margin="223,102,264,0" VerticalAlignment="Top">
        <ComboBoxItem Content="Chandru"/>
        <ComboBoxItem Content="Arul"/>
        <ComboBoxItem Content="Anbu"/>
    </ComboBox>
</Grid>

在上面的代码中,我在combobox中添加了三个项目。 之后我动态地将一个项目添加到同一个组合框中。 这是我写的代码。

public partial class Mycombobox : Window
{
    public Mycombobox()
    {
        this.InitializeComponent();

        // Insert code required on object creation below this point.

        com_friends.Items.Add("Bala");
    }
}

它的输出将是这样的

combobox 但是我想在“Anbu”和“Arul”之间加上“Bala”这个项目。这有可能吗?

请帮帮我......

2 个答案:

答案 0 :(得分:2)

    com_friends.Items.Insert(2,"Bala");

第一个参数是您要插入新对象的索引。

答案 1 :(得分:1)

com_friends.Items.Insert(index, "Bala");

应该完全按照你的意愿行事。

相关问题