[WPF 如何] 如何向 ComboBox 添加一个空白选项
看到這個問題,你可能會蔑視一笑 : 這也能成文章?
確實,你只需要在 ItemsSource 的0位置上插入一個空白的項就是了,如:
1 this.Accounts = rep.All.OrderBy(a => a.Account).Select(a => a.Account).ToList(); 2 this.Accounts.Insert(0, ""); 1 <ComboBox Grid.Column="3" Grid.Row="2" x:Name="Accounts" SelectedItem="{Binding SelectedAccount}" />確實夠簡單,表現的很完美.
?
換成可空的數據源呢?
1 public static List<LogisticsTypes?> DeliveryTypes { 2 get; 3 set; 4 } 5 6 7 public LogisticsTypes? SelectedDeliveryType { 8 get; 9 set; 10 } DeliveryTypes = Enum.GetValues(typeof(LogisticsTypes)).Cast<LogisticsTypes?>().ToList(); DeliveryTypes.Insert(0, null); 1 <ComboBox Grid.Row="1" Grid.Column="5" ItemsSource="{Binding Source={x:Static model:OrderQueryViewModel.DeliveryTypes}}" SelectedItem="{Binding SelectedDeliveryType,Mode=TwoWay}"> 2 <ComboBox.ItemTemplate> 3 <DataTemplate> 4 <TextBlock Text="{Binding . , Converter={StaticResource EnumDesc}}" /> 5 </DataTemplate> 6 </ComboBox.ItemTemplate> 7 </ComboBox>這樣寫很美完美啊!有什么不對勁嗎?如果你說:對,很完美,那就該我蔑視你了!
沒有實踐就沒有"發鹽權", 本文就是說的這個東西.
上面的寫法看似很好,可是: 插入的空白項不能通過鼠標選擇! 只能通過鍵盤才能選擇.
具體為什么, 我也說不出來個所以然來, 見招拆招,見廟拆廟而以, 不遇上它,我也不知道會有這一馬事.
說了這么多廢話,到底有沒有解決辦法呢?
我試了?TargetNullValue,?FallbackValue , 不過這兩個東西是為了解決顯示文本的問題的(不知道說的對不對),不信你可以試試, 它們跟本就沒用,依然無法用鼠標去選擇.
用 CompositeCollection 混合集合
1 <ComboBox Grid.Row="1" Grid.Column="5" SelectedItem="{Binding SelectedDeliveryType}"> 2 <ComboBox.ItemsSource> 3 <CompositeCollection> 4 <ComboBoxItem Content="" /> 5 <CollectionContainer Collection="{Binding Source={x:Static model:OrderQueryViewModel.DeliveryTypes}}" /> 6 </CompositeCollection> 7 </ComboBox.ItemsSource> 8 <ComboBox.ItemTemplate> 9 <DataTemplate> 10 <TextBlock Text="{Binding . , Converter={StaticResource EnumDesc}}" /> 11 </DataTemplate> 12 </ComboBox.ItemTemplate> 13 </ComboBox>嗯, 這下可以選擇空白項了.
只是選擇了空白項后, 接著還有問題 : ?選擇空白項的時候,是不是會有個 紅框框 框住了這個 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 } <ac:CanNullConverter x:Key="CanNull" /> .... .... .... <ComboBox Grid.Row="1" Grid.Column="5" SelectedItem="{Binding SelectedDeliveryType, Converter={StaticResource CanNull}}">?
一切不變,就是在 綁定 SelectedItem 的時候,加了一個 CanNullConverter 這個東西.
好拉,就這些.
謝謝圍觀.?
?
總結
以上是生活随笔為你收集整理的[WPF 如何] 如何向 ComboBox 添加一个空白选项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: day04-硅谷课堂-前端基础知识(二)
- 下一篇: 浅析专题中的构图之美