你如何绑定一个ListBox的的SelectedItem到另一个ListBox的的SelectedItem的财产? [英] How do you bind a ListBox's SelectedItem to another ListBox's SelectedItem's property?

查看:136
本文介绍了你如何绑定一个ListBox的的SelectedItem到另一个ListBox的的SelectedItem的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UI的列表框绑定到列表<通知和GT;通知{搞定;组; }

I have a ListBox in my UI that is bound to List<Notification> Notifications { get; set; }:

<ListBox ItemsSource="{Binding Notifications}"
         DisplayMemberPath="ServiceAddress"
         Name="NotificationsList"/>

的通知都有一个与之相关的过滤器标题:

A Notification has a filter title associated with it:

public class Notification
{
    public string FilterTitle { get; set; }
    public string ServiceAddress { get; set; }
}

我的UI具有绑定到列表与另一个LT列表框; LogFilter&GT;过滤器{搞定;组; }

<ListBox ItemsSource="{Binding Filters}"
         DisplayMemberPath="Title"
         Name="FiltersList"/>

正如你可以猜到,一个LogFilter包含了一个标题:

And as you can guess, a LogFilter contains a Title:

public class LogFilter
{
    public string Title { get; set; }
}

当我选择在我的NotificationsList通知时,我想它来选择根据通知的FilterTitle和LogFilter的标题的映射在我的FiltersList相应LogFilter。你怎么能做到这一点通过绑定?

When I select a Notification in my NotificationsList, I would like it to select the corresponding LogFilter in my FiltersList based on the mapping of the Notification's FilterTitle and the LogFilter's Title. How could you do that through a binding?

推荐答案

所有你需要设置首先 SelectedValuePath 的第一个列表框 FilterTitle

First of all you need to set SelectedValuePath for first listBox to FilterTitle:

<ListBox ItemsSource="{Binding Notifications}"
         DisplayMemberPath="ServiceAddress" 
         Name="NotificationsList"
         SelectedValuePath="FilterTitle"/>

绑定与的SelectedValue第一列表框使用的ElementName 。还需要设置 SelectedValuePath 标题这里还有:

Bind with SelectedValue of first listbox using ElementName. Also you need to set SelectedValuePath to Title here as well:

<ListBox ItemsSource="{Binding Filters}"
         DisplayMemberPath="Title"
         Name="FiltersList"
         SelectedValuePath="Title"
         SelectedValue="{Binding SelectedValue, ElementName=NotificationsList, 
                                 Mode=OneWay}"/>

这篇关于你如何绑定一个ListBox的的SelectedItem到另一个ListBox的的SelectedItem的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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