ListView控件中选择单个项目,WPF [英] ListView selecting single item , wpf

查看:95
本文介绍了ListView控件中选择单个项目,WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,它的项目源是一个列表。我希望用户选择只有一个项目。当我设置列表视图,以单的SelectionMode,用户仍然可以选择多个项目,它似乎像列表视图是要疯了,并选择该用户没有选择...看起来很奇怪的物品......任何人都可以知道什么可能是这个问题?

我上无法粘贴在这里的截图,我没有粘贴选项.....

这是一个XAML -

 < StackPanel中了minWidth =600保证金=0,0,0,10的Horizo​​ntalAlignment =左WIDTH =600>
    <分组框标题=命令队列了borderThickness =0前景=矢菊花>
                        &所述;边界了borderThickness =1.5CornerRadius =10>
                            < ListView控件的SelectionMode =单背景=透明了borderThickness =0利润=5NAME =ListView_CmdQ的ItemsSource ={结合}了maxHeight =450的FontFamily =宋体字号=12 >
                            < /&的ListView GT;
                        < /边框>
                    < /分组框中>
< / StackPanel的>


解决方案

不要在你的列表中的项目出现多次?你有这样的事情之前,我已经看到了这个问题:

  VAR一个=新事物();
变种B =新事物();VAR myList中=新名单<&东西GT;();
myList.Add(一);
myList.Add(二);
myList.Add(一);
myList.Add(二);

如果你要绑定一个的ListView myList中,你会得到你所描述的行为。我觉得基本上这是做的事实在列表中的多个项目匹配的SelectedItem ,所以列表的样式变得有点混乱。围绕它的一种方式是在另一个类来包装每个项目

  VAR myList中=新名单< WrappedThing>();
myList.Add(新WrappedThing((一));
myList.Add(新WrappedThing((b))的;
myList.Add(新WrappedThing((一));
myList.Add(新WrappedThing((b))的;

...这意味着,在列表中每个项目是唯一的,即使它们包裹产品可能不是

i have a listview and it's items source is a list . I want a user to pick only one item . When I set SelectionMode of the listview to single , the user can still select several items and it seems like the listview is going crazy and selects items that user didn't select... looks very strange... can anyone know what could be the problem?

I cann't paste here a screenshot , i don't have the paste option.....

this is a xaml -

<StackPanel MinWidth="600" Margin="0,0,0,10" HorizontalAlignment="Left" Width="600">
    <GroupBox Header="Command Queue" BorderThickness="0" Foreground="CornflowerBlue">
                        <Border BorderThickness="1.5" CornerRadius="10">
                            <ListView SelectionMode="Single" Background="Transparent" BorderThickness="0" Margin="5" Name="ListView_CmdQ" ItemsSource="{Binding}" MaxHeight="450" FontFamily="verdana" FontSize="12">
                            </ListView>
                        </Border>
                    </GroupBox>
</StackPanel>               

解决方案

Do the items in your list appear more than once? I've seen this problem before where you have something like this:

var a = new Thing();
var b = new Thing();

var myList = new List<Thing>();
myList.Add(a);
myList.Add(b);
myList.Add(a);
myList.Add(b);

If you were to bind a ListView to the myList, you'd get the behaviour you've described. I think basically it's to do with the fact that multiple items in the list match the SelectedItem, so the styling of the list gets a bit confused. One way around it is to wrap each item in another class:

var myList = new List<WrappedThing>();
myList.Add(new WrappedThing((a));
myList.Add(new WrappedThing((b));
myList.Add(new WrappedThing((a));
myList.Add(new WrappedThing((b));

... which means that each item in the list is unique, even though the item they're wrapping may not be.

这篇关于ListView控件中选择单个项目,WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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