2009-07-27 40 views
28

Giả sử chúng ta có một DataSource liên kết với một bộ sưu tập từ Cơ sở dữ liệu. Dĩ nhiên không có item null nào cả. Cách thêm mục void vào ComboBox, để người dùng tải đầu tiên sẽ thấy một chuỗi trống. Tôi không muốn thêm đối tượng giả/void vào Bộ sưu tập. Tối ưu trong XAML. Bất kỳ đề xuất nào?ComboBox với mục trống?

+3

Hãy coi chừng, đó là giải pháp cung cấp KHÔNG làm việc với ràng buộc. – Cartesius00

+2

Tôi tìm thấy một cách xung quanh vấn đề ràng buộc xem bài đăng này: http://stackoverflow.com/questions/6446699/how-do-you-bind-a-collectioncontainer-to-a-collection-in-a-view-model – Frinavale

Trả lời

36
<ComboBox Name="myComboBox" Width="200" Background="White">  
    <ComboBox.ItemsSource>  
     <CompositeCollection> 
      <ComboBoxItem IsEnabled="False" Foreground="Black">Select Item</ComboBoxItem> 
      <CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />  
     </CompositeCollection> 
    </ComboBox.ItemsSource> 
</ComboBox> 

EDIT

Như @surfen nêu trong bình luận, BindingProxy là cách giải quyết cho vấn đề ràng buộc

+0

OK tuyệt vời. Nhưng làm thế nào để làm cho món hàng đầu tiên không thể chọn được? Chỉ có thể chọn các mục nguồn dữ liệu. –

+2

Xem bài viết đã chỉnh sửa, bạn chỉ cần thêm IsEnabled = "False" Foreground = "Black" vào các thuộc tính của mục –

+0

OK xin lỗi tôi đã không nhận thấy các thuộc tính đó. Cảm ơn bạn ! –

2
<UserControl.Resources> 
    <CollectionViewSource x:Key="Modules" Source="{Binding Path=Modules}" /> 
</UserControl.Resources> 

<abv:ComboBox SelectedIndex="0" IsNullable="True" 
    SelectedItem="{Binding Path=SelectedModule, Mode=TwoWay}"> 
    <abv:ComboBox.ItemsSource> 
     <CompositeCollection> 
      <ComboBoxItem Content="{DynamicResource EmptyModuleComboBox}"/> 
      <CollectionContainer Collection="{Binding Source={StaticResource Modules}}" /> 
     </CompositeCollection> 
    </abv:ComboBox.ItemsSource> 
</abv:ComboBox> 

public class ComboBox : System.Windows.Controls.ComboBox 
{ 
    public static readonly DependencyProperty IsNullableProperty = 
     DependencyProperty.Register("IsNullable", typeof(bool), typeof(ComboBox)); 

    public bool IsNullable 
    { 
     get { return (bool)GetValue(IsNullableProperty); } 
     set { SetValue(IsNullableProperty, value); } 
    } 

    public ComboBox() 
    { 
     Loaded += ComboBox_Loaded; 
    } 

    void ComboBox_Loaded(object sender, RoutedEventArgs e) 
    { 

     if (IsNullable) 
     { 
      this.ItemContainerStyle = new Style(); 

      this.ItemContainerStyle.Setters.Add(new EventSetter() 
      { 
       Event = ComboBoxItem.PreviewMouseUpEvent, 
       Handler = new MouseButtonEventHandler(cmbItem_PreviewMouseUp) 
      }); 
     } 
    } 

    public void cmbItem_PreviewMouseUp(object sender, MouseButtonEventArgs e) 
    { 
     if (Items.IndexOf(sender as ComboBoxItem) == 0) 
     { 
      SelectedItem = null; 
     } 
    } 
} 
+1

Bạn có thể giải thích tại sao điều này không công trinh? – nikaltipar

+0

Điều này hoạt động vì đăng ký sự kiện di chuột lên –

1

Đối với ràng buộc đối với MVVM đối tượng:

     <ComboBox Name="cbbFiltres" SelectedItem="{Binding ElmtInfo, Mode=TwoWay}" Height="26" MinWidth="90" SelectedIndex="0" SelectedValuePath="Id"> 
         <ComboBox.Resources> 
          <CollectionViewSource x:Key="cvsFiltres" Source="{Binding Elmts.items}"/> 
         </ComboBox.Resources> 
         <ComboBox.ItemsSource> 
          <CompositeCollection> 
           <model:tblFiltreChamps Desc="{x:Static resx:resMain.enumAucun}" Id="0"/> 
           <CollectionContainer Collection="{Binding Source={StaticResource cvsFiltres}}" /> 
          </CompositeCollection> 
         </ComboBox.ItemsSource> 
        </ComboBox> 

Và đối với ràng buộc trên:

<Label Visibility="{Binding Path=SelectedValue, ElementName=cbbFiltres, Converter={StaticResource NullToVisibility}}" /> 

Và bộ chuyển đổi chung:

public class ConvNullToVisibility : IValueConverter { 
    /// <summary>Convertisseur pour le Get.</summary> 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
     if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return Visibility.Visible; // Pour annuler l'effet dans le designer: http://stackoverflow.com/questions/33401900/wpf-detect-design-mode-in-a-converter 
     return ((value == null) || (string.IsNullOrEmpty(value.ToString())) || (value.ToString() == "0")) ? Visibility.Collapsed : Visibility.Visible; 
    } 

    /// <summary>Convertisseur inverse, pour le Set (Binding).</summary> 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
     if (value is Visibility) { 
      return (((Visibility)value) == Visibility.Visible) ? true : false; 
     } else return false; 
    } 
} 

Chỉ cần quan trọng để khai báo các SelectedValuePath trong combobox. :-)

0

Thử hộp tổ hợp Mahapps.

xmlns: điều khiển = "http://metro.mahapps.com/winfx/xaml/controls"

<ComboBox x:Name="bars" **controls:TextBoxHelper.ClearTextButton="True"** 
       DisplayMemberPath="Name" 
       Height="21" 
       SelectedItem="{Binding Bar}"/> 

Combo Box View

Các vấn đề liên quan