ComboBox与空项目?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ComboBox与空项目?相关的知识,希望对你有一定的参考价值。

假设我们有一个DataSource绑定到Database的集合。当然没有空项。如何将void项添加到ComboBox中,以便在第一次加载用户时会看到一个空字符串。我不想在Collection中添加一个dummy / void对象。最适合XAML。有什么建议?

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

编辑

正如@surfen在评论中提到的那样,BindingProxy是绑定问题的解决方法

另一答案
<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;
        }
    }
}
另一答案

对于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非常重要。 :-)

另一答案

试试Mahapps组合框。

的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

以上是关于ComboBox与空项目?的主要内容,如果未能解决你的问题,请参考以下文章

选择 ComboBox 项目会冻结应用程序

从 C# 中的 Combobox 中选择特定项目时,将 Label 值增加 1

如何设置combobox 下拉框的内容

ExtJS ComboBox 不会显示项目

在delphi中如何用combobox实现分级读取数据库中的内容

如何避免实体框架与空/空数据库的连接