博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[WPF 如何] 如何向 ComboBox 添加一个空白选项
阅读量:7188 次
发布时间:2019-06-29

本文共 4707 字,大约阅读时间需要 15 分钟。

原文:

看到这个问题,你可能会蔑视一笑 : 这也能成文章?

确实,你只需要在 ItemsSource 的0位置上插入一个空白的项就是了,如:

1                 this.Accounts = rep.All.OrderBy(a => a.Account).Select(a => a.Account).ToList();2                 this.Accounts.Insert(0, "");
1 

确实够简单,表现的很完美.

 

换成可空的数据源呢?

1         public static List
DeliveryTypes { 2 get; 3 set; 4 } 5 6 7 public LogisticsTypes? SelectedDeliveryType { 8 get; 9 set;10 }
DeliveryTypes = Enum.GetValues(typeof(LogisticsTypes)).Cast
().ToList();DeliveryTypes.Insert(0, null);
1 
2
3
4
5
6
7

这样写很美完美啊!有什么不对劲吗?如果你说:对,很完美,那就该我蔑视你了!

没有实践就没有"发盐权", 本文就是说的这个东西.

上面的写法看似很好,可是: 插入的空白项不能通过鼠标选择! 只能通过键盘才能选择.

具体为什么, 我也说不出来个所以然来, 见招拆招,见庙拆庙而以, 不遇上它,我也不知道会有这一马事.

说了这么多废话,到底有没有解决办法呢?

我试了 TargetNullValue, FallbackValue , 不过这两个东西是为了解决显示文本的问题的(不知道说的对不对),不信你可以试试, 它们跟本就没用,依然无法用鼠标去选择.

用 CompositeCollection 混合集合

1                 
2
3
4
5
6
7
8
9
10
11
12
13

嗯, 这下可以选择空白项了.

只是选择了空白项后, 接着还有问题 :  选择空白项的时候,是不是会有个 红框框 框住了这个 ComboBox ? 像这样:

为什么呢? 看到 SelectedItem 了没有? 选择的空白选项是个 ComboBoxItem 

System.Windows.Data Error: 23 : Cannot convert 'System.Windows.Controls.ComboBoxItem' from type 'ComboBoxItem' to type 'System.Nullable`1[AsNum.Aliexpress.Entity.LogisticsTypes]' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: EnumConverter 无法从 System.Windows.Controls.ComboBoxItem 转换。

在 System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
在 System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
在 System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
在 System.ComponentModel.NullableConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
在 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 无法从 System.Windows.Controls.ComboBoxItem 转换。
在 MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
在 MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
在 System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'

是的, 转换为 SelectedItem 的时候失败了.

疙瘩,一波未平一波又起. 是用 CompositeCollection 还是用 Insert ? 一个不能选择空白项, 一个可以选, 但是选了也白选.

纠结,郁闷,没人可以问, 我劝你牙的就别百度了, 百了也没用 (好像根本就没有相关的文章是中文的!这不能愿百度). 

搜英文?怎么组织关键词? 算鸟, 即然这样,就换个角度吧. 用 Insert 铁定不行(别喷我噢,我确实没有找出来可行的办法), 用 CompositeCollection 很接近解决办法了,只是转换的时候出了点小问题而以,不行就加个 Converter 呗.

记住一句话:不动手就没发盐权

1     public class CanNullConverter : IValueConverter { 2         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 3             return value; 4         } 5  6         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 7             NullableConverter nullableConvert; 8             var toType = targetType; 9             if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {10                 nullableConvert = new NullableConverter(targetType);11                 toType = nullableConvert.UnderlyingType;12             }13 14             return value.GetType().Equals(toType) ? value : null;15         }16     }
............

 

一切不变,就是在 绑定 SelectedItem 的时候,加了一个 CanNullConverter 这个东西.

好拉,就这些.

谢谢围观. 

 

转载地址:http://ckfkm.baihongyu.com/

你可能感兴趣的文章
Perl语言之统计特定字符串个数
查看>>
Linux用户和组管理
查看>>
Eclipse中从svn上检出项目无法识别jar包
查看>>
shell实现FTP自动批量上传下载文件
查看>>
我的友情链接
查看>>
Mybatis使用存储过程以及原理
查看>>
通讯录二维码使英文变为中文
查看>>
快2012了!
查看>>
【Objective-C】OC中字典基本概念和常用方法(NSDictionary和NSMutableDictionary)
查看>>
maven打包包含配置文件
查看>>
C# readonly与const比较
查看>>
我的友情链接
查看>>
我的大学n糊口
查看>>
设置easyUi弹出窗在弹出框(iframe)中不居中问题
查看>>
&.nbsp;在IE和谷歌浏览器中不兼容的解决办法
查看>>
Servlet容器
查看>>
捷为科技签约镇海石化, 打造石化建安行业生产项目信息化管理系统
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Java异常
查看>>