WPF Single selection between two ListBoxes(WPF 两个列表框之间的单选)
问题描述
我遇到以下问题:我有两个 ListBox
,有两个不同的 ItemSource
,但它们都有相同的 binding
对于 SelectedItem
,因为我试图在这两个列表之间执行单个选择.
I'm having the following problem: I have two ListBox
, with two different ItemSource
, but both of them have the same binding
for the SelectedItem
, because I was trying to perform a single selection between these two lists.
这是一张更能说明问题的图片:
Here's an image that better shows the problem:
我想做什么?每次我从第一个列表(红色)中选择一个项目时,它应该取消选择第二个列表(黑色)中的 SelectedItem
,反之亦然.这就是为什么我对它们都使用相同的 binding
的原因.我真的不知道这是否是更好的方法,但它应该像那样工作.
What would I like to do? Every time I select one item from the first list (in red), it should deselect the SelectedItem
from the second list (in black), and vice versa. That's why I'm using the same binding
for both of them.
I really don't know if it's the better way to do it, but it should work like that.
你能帮帮我吗?
推荐答案
我要做的是首先将 null
传递给属性并通知更改,然后将实际值传递给属性并通知更改视图.像这样:
What I had to do was at first pass null
to the property and notify the changing, and then I passed the real value to the property and notified the changing to the view.
Like that:
protected Bar selectedItem;
public Bar SelectedItem{
get
{
return selectedItem;
}
set
{
selectedItem = null;
NotifyPropertyChanged("SelectedItem");
selectedItem = value;
NotifyPropertyChanged("SelectedItem");
}
我从 this question 中得到了这个答案和示例.
I got this answer and example from this question.
这篇关于WPF 两个列表框之间的单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WPF 两个列表框之间的单选


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01