WPF 两个列表框之间的单选 [英] WPF Single selection between two ListBoxes

查看:15
本文介绍了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 两个列表框之间的单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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