为什么我不能在ComboBox中选择一个空值? [英] Why can't I select a null value in a ComboBox?

查看:1007
本文介绍了为什么我不能在ComboBox中选择一个空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,似乎无法从ComboBox选择(使用鼠标)一个null值。 编辑要澄清,这是.NET 3.5 SP1。

In WPF, it seems to be impossible to select (with the mouse) a "null" value from a ComboBox. Edit To clarify, this is .NET 3.5 SP1.

这里有一些代码来显示我的意思。首先,C#声明:

Here's some code to show what I mean. First, the C# declarations:

public class Foo
{
    public Bar Bar { get; set; }
}

public class Bar 
{
    public string Name { get; set; }
}

接下来,我的Window1 XAML:

Next, my Window1 XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox x:Name="bars" 
                  DisplayMemberPath="Name" 
                  Height="21" 
                  SelectedItem="{Binding Bar}"
                  />
    </StackPanel>
</Window>

最后,我的Window1类:

And lastly, my Window1 class:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        bars.ItemsSource = new ObservableCollection<Bar> 
        {
            null, 
            new Bar { Name = "Hello" }, 
            new Bar { Name = "World" } 
        };
        this.DataContext = new Foo();
    }
}

我有一个ComboBox的项目绑定到一个Bar实例列表,其中之一为null。我已将窗口绑定到Foo的实例,并且ComboBox显示其Bar属性的值。

With me? I have a ComboBox whose items are bound to a list of Bar instances, one of which is null. I have bound the window to an instance of Foo, and the ComboBox is displaying the value of its Bar property.

当我运行此应用程序时,ComboBox以空显示,因为Foo.Bar默认为null。没关系。如果我使用鼠标放下ComboBox并选择Hello项目,这也工作。但是如果我尝试重新选择列表顶部的空项目,则ComboBox关闭并返回到之前的值Hello!

When I run this app, the ComboBox starts with an empty display because Foo.Bar is null by default. That's fine. If I use the mouse to drop the ComboBox down and select the "Hello" item, that works too. But then if I try to re-select the empty item at the top of the list, the ComboBox closes and returns to its previous value of "Hello"!

空值与箭头键工作原理正如所料,并设置它的程序工作。

Selecting the null value with the arrow keys works as expected, and setting it programatically works too. It's only selecting with a mouse that doesn't work.

我知道一个简单的解决方法是让一个Bar的实例代表null,并通过一个IValueConverter运行它,但是

I know an easy workaround is to have an instance of Bar that represents null and run it through an IValueConverter, but can someone explain why selecting null with the mouse doesn't work in WPF's ComboBox?

推荐答案

无效的项目可以解释为什么用鼠标选择null不能在WPF的ComboBox中工作? 。这是为什么在使用键盘选择空项之后,您可以使用键盘选择空项,而不是选择任何后续项。之后无法重新选择先前选择的项目(Hello) - 除非通过鼠标!

The null "item" is not being selected by the keyboard at all - rather the previous item is being unselected and no subsequent item is (able to be) selected. This is why, after "selecting" the null item with the keyboard, you are thereafter unable to re-select the previously selected item ("Hello") - except via the mouse!

项目。当您认为自己正在这样做时,您可以取消选择或选择上一个或新项目。

这可能最好通过添加背景到组合框中的项目。当您选择Hello时,您会注意到ComboBox中的彩色背景,但是当您通过键盘取消选择它时,背景颜色会消失。我们知道这不是null项,因为null项实际上有背景颜色,当我们通过鼠标放下列表!

This can perhaps best be seen by adding a background to the items in the ComboBox. You will notice the colored background in the ComboBox when you select "Hello", but when you deselect it via the keyboard, the background color disappears. We know this is not the null item, because the null item actually has the background color when we drop the list down via the mouse!

以下XAML,从在原始问题中,将在项目后面放置LightBlue背景,以便您可以看到此行为。

The following XAML, modified from that in the original question, will put a LightBlue background behind the items so you can see this behavior.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox x:Name="bars" Height="21" SelectedItem="{Binding Bar}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="LightBlue" Width="200" Height="20">
                        <TextBlock Text="{Binding Name}" />
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>
</Window>

如果你想进一步验证,你可以处理ComboBox上的SelectionChanged事件, null item实际上在其SelectionChangedEventArgs中提供了一个空数组的AddedItems,并且通过用鼠标选择Hello来取消选择null项目给出一个空数组RemovedItems。

If you want further validation, you can handle the SelectionChanged event on the ComboBox and see that "selecting the null item" actually gives an empty array of AddedItems in its SelectionChangedEventArgs, and "deselecting the null item by selecting 'Hello' with the mouse" gives an empty array of RemovedItems.

这篇关于为什么我不能在ComboBox中选择一个空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆