[WPF How to] add a blank option to a ComboBox

Source: Internet
Author: User

Original: [WPF how to] add a blank option to a ComboBox

Seeing this problem, you may be contemptuous of a smile: Can this also be an article?

Indeed, you just need to insert a blank entry in the 0 position of ItemsSource, such as:

1                 this. Accounts = Rep. All.orderby (A = A.account). Select (a = a.account). ToList (); 2                 this. Accounts.insert (0"");
1 <  grid.column= "3"  grid.row= "2"  x:name= "Accounts" SelectedItem =" {Binding Selectedaccount} " />

It's simple enough, it's perfect.

To a nullable data source?

1          Public StaticList<logisticstypes?>Deliverytypes {2             Get;3             Set;4         }5 6 7          PublicLogisticstypes?Selecteddeliverytype {8             Get;9             Set;Ten}
Deliverytypes = Enum.getvalues (typeof (Logisticstypes)). Cast<logisticstypes? > (). ToList ();D eliverytypes.insert (0, null);
1 <ComboBoxGrid.Row= "1"Grid.column= "5"ItemsSource="{Binding source={x:static model:OrderQueryViewModel.DeliveryTypes}}"SelectedItem="{Binding Selecteddeliverytype,mode=twoway}">2                     <combobox.itemtemplate>3                         <DataTemplate>4                                 <TextBlockText="{Binding., Converter={staticresource Enumdesc}}" />5                         </DataTemplate>6                     </combobox.itemtemplate>7                 </ComboBox>

It's beautiful and perfect! What's wrong with that? If you say: Yes, perfect, then I despise you!

There is no "salt right"without practice, this is what this article means.

The above writing seems very good, but: inserted blank items can not be selected by the mouse! Only the keyboard can be selected.

Specifically why, I also can not say why come, see recruit demolition recruit, see Temple Demolition Temple and, on it, I do not know there will be this horse thing.

Having said so much nonsense, is there any way out?

I tried Targetnullvalue, Fallbackvalue, but these two things are to solve the problem of displaying text (do not know the right), do not believe you can try, they are useless, still can not use the mouse to choose.

Mixing collections with CompositeCollection

1                 <ComboBoxGrid.Row= "1"Grid.column= "5"SelectedItem="{Binding Selecteddeliverytype}">2                     <Combobox.itemssource>3                         <compositecollection>4                             <ComboBoxItemContent="" />5                             <CollectioncontainerCollection="{Binding source={x:static model:OrderQueryViewModel.DeliveryTypes}}" />6                         </compositecollection>7                     </Combobox.itemssource>8                     <combobox.itemtemplate>9                         <DataTemplate>Ten                             <TextBlockText="{Binding., Converter={staticresource Enumdesc}}" /> One                         </DataTemplate> A                     </combobox.itemtemplate> -                 </ComboBox>

Well, you can choose a blank item.

Just after selecting the blank item, then there is the question: When you select a blank item, will there be a red frame to hold the ComboBox? Like this:

Why is it? Did you see SelectedItem? The blank option selected is a ComboBoxItem

System.Windows.Data error:23:cannot convert ' System.Windows.Controls.ComboBoxItem ' from type ' comboboxitem ' to type ' Sy Stem. Nullable ' 1[asnum.aliexpress.entity.logisticstypes] ' for ' en-us ' culture with default conversions; Consider using Converter property of the Binding. NotSupportedException: ' System.NotSupportedException:EnumConverter cannot be converted from System.Windows.Controls.ComboBoxItem.
In System.ComponentModel.TypeConverter.GetConvertFromException (Object value)
In System.ComponentModel.TypeConverter.ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, Object value)
In System.ComponentModel.EnumConverter.ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, Object value)
In System.ComponentModel.NullableConverter.ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, Object Value
In MS. Internal.Data.DefaultValueConverter.ConvertHelper (Object o, Type destinationtype, DependencyObject targetelement, CultureInfo culture, Boolean Isforward) '
System.Windows.Data Error:7: convertback cannot convert value ' System.Windows.Controls.ComboBoxItem ' (type ' ComboBoxItem '). Bindingexpression:path=selecteddeliverytype; Dataitem= ' Orderqueryviewmodel ' (hashcode=10859455); Target element is ' ComboBox ' (name= '); Target property is ' SelectedItem ' (type ' Object ') NotSupportedException: ' System.NotSupportedException:EnumConverter Unable to convert from System.Windows.Controls.ComboBoxItem.
In MS. Internal.Data.DefaultValueConverter.ConvertHelper (Object o, Type destinationtype, DependencyObject targetelement, CultureInfo culture, Boolean Isforward)
In MS. Internal.Data.ObjectTargetConverter.ConvertBack (Object o, type type, object parameter, CultureInfo culture)
In System.Windows.Data.BindingExpression.ConvertBackHelper (IValueConverter Converter, Object value, Type sourcetype, Object parameter, CultureInfo culture) '

Yes, the conversion to SelectedItem failed.

pimple, cue unravelling. Are you using CompositeCollection or Insert? One cannot select a blank item, one can be selected, but the selection is also white.

Tangled, depressed, no one can ask, I advise you do not have the teeth Baidu, hundred also useless (as if there is no relevant article is Chinese!). This can not be willing Baidu.

How to organize keywords in English? Count the birds, that's it, just change the angle. With Insert is not sure (do not spray me oh, I really did not find out the feasible way), with CompositeCollection very close to the solution, but only when the conversion of a little problem and to, not to add a Converter bai.

Remember a word: do not do not send salt right

1 Public class Cannullconverter:ivalueconverter {2 Public Object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo Cultur e) {3 return value;4         }5 6 Public Object Convertback (object value, Type targetType, object parameter, System.Globalization.CultureInfo CU Lture) {7 Nullableconverter Nullableconvert;8 var totype = TargetType;9if (Targettype.isgenerictype && targettype.getgenerictypedefinition (). Equals (typeof (Nullable<>))) {Ten Nullableconvert = new Nullableconverter (targetType); One totype = Nullableconvert.underlyingtype; A             } -  - return value. GetType (). Equals (ToType)? Value:null; the         } -}
<x:key= "Cannull"/> ...... <  grid.row= "1"  grid.column= "5"  selecteditem= "  {Binding selecteddeliverytype, Converter={staticresource cannull}}'>

All the same, in the binding SelectedItem, add a cannullconverter this thing.

Good pull, that's all.

Thanks for the onlookers.

[WPF How to] add a blank option to a ComboBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.