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

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

问题描述

在 WPF 中,似乎不可能(用鼠标)从 ComboBox 中选择空"值.编辑澄清一下,这是 .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 实例列表,其中一个为空.我已将窗口绑定到 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 默认为空.没关系.如果我使用鼠标放下 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 运行它,但是有人可以解释为什么在 WPF 的 ComboBox 中用鼠标选择 null 不起作用吗?

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?

推荐答案

键盘根本没有选择空项目"——而是取消选择前一个项目并且没有后续项目(能够 这就是为什么在用键盘选择"空项目后,您无法重新选择之前选择的项目(你好")——除非通过鼠标!

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!

简而言之,您既不能选择也不能取消选择 ComboBox 中的空项目.当您认为这样做时,您实际上是在取消选择或选择上一个或新项目.

这可能最好通过为 ComboBox 中的项目添加背景来体现.当您选择Hello"时,您会注意到 ComboBox 中的彩色背景,但是当您通过键盘取消选择它时,背景颜色将消失.我们知道这不是空项,因为当我们通过鼠标下拉列表时,空项实际上具有背景颜色!

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 将在项目后面放置一个浅蓝色背景,以便您可以看到此行为.

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 事件,并看到选择空项"实际上在它的 SelectionChangedEventArgs 中给出了一个空的 AddItems 数组,并通过选择 'Hello' 来取消选择空项鼠标"给出了一个空的 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天全站免登陆