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 两个列表框之间的单选
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
