怎样在VS2015C#WPF中combobox下拉菜单中添加内容?不要数据绑定

怎样在VS2015C#WPF中combobox下拉菜单中添加内容?不要数据绑定,第1张

不要数据绑定的话那就这样写啊则陆

<ComboBox Margin="10" Width="150" SelectedIndex="0" Name="combobox">

    <ComboBoxItem Content="-请选择-"><孙埋顷/ComboBoxItem>

    <ComboBoxItem Content="数据1"></ComboBoxItem>

    <ComboBoxItem Content="数据2"></ComboBoxItem>

    <ComboBoxItem Content="数液蠢据3"></ComboBoxItem>

</ComboBox>

做个例子,希望有所帮助。根据要求,首先在Form_Load设置combox的内容掘咐雹,然后点击按钮后添加一项进去。实际上是做了数据的更新。 代码内容private void button1_Click(object sender, EventArgs e)

{

//添加一项,修改判帆内容

List<string>temp1 = (List<string>)this.comboBox1.DataSource

List<string>temp2 = new List<string>()

temp2.Add("请选择")

foreach (string str in temp1)

{

temp2.Add(str)

}

this.comboBox1.DataSource = temp2

} private void Form1_Load(object sender, EventArgs e)

{

//初始化combox选择项,设置选择内容

List<string>comString = new List<string>()

for (int i = 0i <5i++)

{

comString.Add(string.Format("选项{0}", i))

}

this.comboBox1.DataSource = comString

} 结果简悉图示


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11991295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存