绑定后在组合框上显示默认文本. [英] Display Default text on combobox after binding.

查看:68
本文介绍了绑定后在组合框上显示默认文本.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一个绑定框,该组合框绑定到填充报告列表的WCF服务.
我不想立即显示项目,我希望所选索引显示
请从列表中选择一个报告".
因此,我创建了一个项目并将其添加到ComboBox.
我将控件绑定为
后的项目列表

Hello,
I have a combobox bound to a WCF service that populates a list of reports.
I don''t want to display an item immidiately, I want the selected index to say
"Please select a report from list".
So I created an item and added it to the ComboBox.
Items List after I bound the control as

comboBox1.ItemsSource = myData;
                    comboBox1.DisplayMemberPath = "Product_Name";
                    comboBox1.SelectedValuePath = "Product_Name";
                    comboBox1.SelectedValue = "ProdID";

                    ComboBoxItem item = new ComboBoxItem();
                   
                    item.Content = "Select";
                    item.Tag = "1";

                    comboBox1.Items.Add(item);
                    //comboBox1.Items.Insert(0, item);



但却给出错误消息,提示只读集合不支持该操作"
有没有办法在组合框上显示默认文本?

在此先感谢您.



But it gives error message that "operation not supported on read-only collection"
Is there a way to display default text on a combobox?

Thanks in advance.

推荐答案

尝试一下,我已经尝试过并且工作正常

Try this, i have tried it and working well

List<Products> result1 = new List<Products>();

                    Products obj = new Products();

                    obj.ProdID = 0;

                    obj.Product_Name= "Select";
                    result1.Add(obj);
                    result1.AddRange(myData);

                    comboBox1.ItemsSource = result1;

                    comboBox1.DisplayMemberPath = "Product_Name";
                    comboBox1.SelectedValuePath = "Product_Name";
                    comboBox1.SelectedValue = "ProdID";

                    comboBox1.SelectedIndex = 0;


亲爱的兄弟,
最简单的方法如下:

在包含您的组合框的表单上编写以下代码.

私有void frmMainForm_Load(对象发送者,EventArgs e)
{
cmbDepartment.SelectedIndex = 0;
cmbUniversity.SelectedIndex = 0;
}
还要确保您在组合框的第一个菜单上写了"[Select Any]".
我自己设计了一个,发现它很完美.
Dear Brother,
Simplest way of doing so is following:

Write the following code on the form containing your combobox.

private void frmMainForm_Load(object sender, EventArgs e)
{
cmbDepartment.SelectedIndex = 0;
cmbUniversity.SelectedIndex = 0;
}
Also make sure u have written "[Select Any]" on the first menu in the combobox.
I ''ve devised this myself and found it perfect.


这篇关于绑定后在组合框上显示默认文本.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆