ComboBox z pustym przedmiotem?

Załóżmy, że mamy DataSource bind do kolekcji z bazy danych. Nie ma oczywiście elementu null. Jak dodać void element do Comboboxu , aby przy pierwszym załadowaniu użytkownik zobaczył pusty ciąg znaków. Nie chcę dodawać obiektu dummy/void do kolekcji. optymalnie w XAML . Jakieś propozycje?

Author: Tshepang, 2009-07-27

4 answers

<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

Jako @ surfen wspomniany w komentarzu, BindingProxy jest obejściem problemu wiążącego

 41
Author: Arsen Mkrtchyan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:25:31
<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;
        }
    }
}
 2
Author: Андрей Мосальский,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-02 07:52:01

Do wiązania na obiekcie 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>

I do wiązania na:

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

I konwerter generyczny:

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

Tylko ważne, aby zadeklarować SelectedValuePath w combobox. :-)

 1
Author: david,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-07 08:32:29

Spróbuj ComboBox Mahapps.

Xmlns: controls="http://metro.mahapps.com/winfx/xaml/controls"

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

Widok Pola Combo

 0
Author: Anurag,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-25 08:29:25