列表框的选择,即使在的SelectionMode许多项目="单" [英] ListBox is selecting many items even in SelectionMode="Single"

查看:178
本文介绍了列表框的选择,即使在的SelectionMode许多项目="单"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也遇到过一些很奇怪的,简单的WPF应用程序

I have encountered something very strange, simple WPF application

<Window x:Class="ListBoxSelection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding Path=Strings}" SelectionMode="Single"/>
    </Grid>
</Window>

背后

public class ViewModel
{
    public List<string> Strings { get; set; }

    public ViewModel ()
    {
        Strings = new List<string> ();
        Strings.Add ("A");
        // add many items ...
        Strings.Add ("A");
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow ()
    {
        InitializeComponent ();

        DataContext = new ViewModel ();
    }
}

当我点击一个单一的项目,

and when I click on a single item,

如果我继续单击项目,他们就聚集。单击已选定的项目不执行任何操作。抓我的头,我数据绑定列表,以列表框之前,以前从来没有见过这个。运行Win7的(64),VS2010,行为presents与.net 3.5,NET 3.5的客户端配置文件,.NET 4.0和.Net 4客户端配置文件。

if I continue clicking items, they just aggregate. Clicking an already selected item does nothing. Scratching my head, I have databound lists to ListBoxes before, and have never seen this before. Running Win7 (64), VS2010, behaviour presents with .Net 3.5, .Net 3.5 Client Profile, .Net 4, and .Net 4 Client Profile.

精氨酸,我要提到我期待着正常的,默认情况下,单选行为。

Arg, I should mention I am expecting normal, default, single-select behaviour.

推荐答案

丹科比得到了大多数他的评论的回答。

Dan Bryant got most of the answer in his comment.

什么是怎么回事是字符串实习。当您创建一批具有相同值的字符串,净由具有相同的字符串值的所有引用可以节省内存的使用实际上是指同一个字符串对象。 (见这个,例如,了解详细信息。)

What's going on here is string interning. When you create a bunch of strings with the same value, .Net saves on memory usage by having all references to the same string value actually refer to the same string object. (See this, for instance, for details.)

我真的不知道为什么在列表框的行为完全它的方式,这是你第一次在列表中选择任何项目,也同时选择该项目,并在列表中的第一项。不过,这并不当你点击一个新的项目,因为检查,看是否的SelectedItem 是您刚才点击的项目不同的取消选择,而事实并非如此。

I don't really know why the ListBox behaves exactly the way it does, which is that the first time you select any item in the list, it selects both that item and the first item in the list. But it doesn't unselect when you click on a new item because checks to see if the SelectedItem is different from the item you just clicked on, and it isn't.

我由一个列表框结合到测试对象的集合得到完全相同的行为

I got exactly the same behavior by binding a ListBox to a collection of test objects:

public class TestObject
{
    public override string ToString()
    {
        return GetHashCode().ToString();
    }
}

MainWindow.xaml

<ListBox x:Name="MyListBox" ItemsSource={Binding}"/>

MainWindow.xaml.cs

ObservableCollection<TestObject> test = new ObservableCollection<TestObject>();
TestObject t = new TestObject();
test.Add(t);
test.Add(t);
test.Add(t);
test.Add(t);
test.Add(t);
test.Add(t);
MyListBox.DataContext = test;

这篇关于列表框的选择,即使在的SelectionMode许多项目=&QUOT;单&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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