WPF。列表框。如何最大限度所选项目的限制为2? [英] WPF. ListBox. How to limit maximum of selected items to 2?

查看:110
本文介绍了WPF。列表框。如何最大限度所选项目的限制为2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要限制在ListBox到2所选项目的数量如何做到这一点?是否有可能避免事件的使用,并为此在XAML?

I need to limit the number of selected items in a ListBox to 2. How to do this? Is it possible to avoid the usage of events and do this in XAML?

推荐答案

有没有内置的方式做到这一点,据我所知,所以你将不得不写一些code。

There's no built-in way to do this as far as I'm aware, so you're going to have to write some code.

您可以通过设置的SelectionMode 设置你的ListBox的多选到扩展(见这里的区别),并附着在的SelectionChanged 事件。当事件发生时,修改你怎么认为合适的选择(你如何做到这将取决于你如何想它的工作...如果他们选择第三个,它得到拆除,对吗?还是第一个拿到去除 - 有效先进先出或后进先出法去除)

You could set your ListBox as multi-select by setting SelectionMode to Multiple or Extended (See here for the difference) and attach onto the SelectionChanged event. When the event is raised, modify the selection how you see fit (how you do it would depend on how you wanted it to work... if they select a third one, does it get removed right away? or does the first one get removed - effectively FIFO or LIFO removal).

编辑:

我的坏,我已经联系到了错误的文章。

My bad, I had linked to the wrong article.

这篇关于WPF。列表框。如何最大限度所选项目的限制为2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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