简体   繁体   English

组合框绑定和空项目

[英]Combobox binding and empty item

I am writing a search form for desktop application and I have a problem with combobox binding. 我正在为桌面应用程序编写搜索表单,并且组合框绑定有问题。
The user can search for properties by various criteria (city, price, etc). 用户可以通过各种条件(城市,价格等)搜索属性。
I want to bind combobox to list of all possible cities, but I want to leave the user the option not to choose anything so it can search properties in all cities. 我想将组合框绑定到所有可能城市的列表,但是我想让用户选择不选择任何内容,以便它可以搜索所有城市的属性。
How can I do that? 我怎样才能做到这一点?
I'm using linq2sql for data access. 我正在使用linq2sql进行数据访问。

Well, supposing you have bound the combobox to a List of Cities that you pulled from your database: 好吧,假设您已将组合框绑定到从数据库中拉出的城市列表:

class City
{
   public string ID{get; private set;}
   public string Name{get;private set;}

   public City(string id, string name)
   {
       ID=id;Name=name;
   }  
}

Just Add an empty city to that list: 只需在该列表中添加一个空城市:

cityList.Add(new City("AA",string.empty");

When you bind the combobox to the cityList and sort it, the city with id "AA" will be on top. 当您将组合框绑定到cityList并对其进行排序时,ID为“ AA”的城市将位于顶部。 In your searching code you can now adjust your search algorithm for the case that the user has selected the city with id "AA"; 现在,在您的搜索代码中,您可以针对用户选择了ID为“ AA”的城市来调整搜索算法;

Cleverer solutions are certainly possible, but I would need to see some code of the searching method to help you further. 当然可以使用更聪明的解决方案,但是我需要查看搜索方法的一些代码以进一步帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM