2009-07-27 47 views
28

假設我們有一個DataSource綁定到來自數據庫的集合。當然沒有空項目。如何將void項添加到組合框中,以便在第一次加載用戶時會看到一個空字符串。我不想在集合中添加一個虛擬/無效對象。 最好在XAML。任何建議?與空項目的組合框?

+3

當心,所提供的解決方案不具有結合工作。 – Cartesius00 2011-05-23 19:00:05

+2

我找到了解決綁定問題的方法,請參閱此帖:http://stackoverflow.com/questions/6446699/how-do-you-bind-a-collectioncontainer-to-a-collection-in-a-view-model – Frinavale 2011-06-23 15:59:45

回答

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> 

編輯

正如在評論中提及@surfenBindingProxy是綁定問題

+0

好的。但如何使第一個項目不可選?只能選擇數據源項目。 – 2009-07-27 14:31:15

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

變通方法MVVM對象綁定:

     <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> 

而對於綁定上:

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

和通用轉換器:

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; 
    } 
} 

只是重要聲明SelectedValuePath在組合框中。 :-)

0

試試Mahapps combobox。

的xmlns:對照= 「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