简体   繁体   English

WPF中的绑定字典

[英]binding dictionary in WPF

i have a dictonary 我有一本字典

public class TAGs :INotifyPropertyChanged{
private Dictionary<string, string[]> _items = new Dictionary<string,string[]>();
 ...............
public string[] Keys
    {
        get { return Items.Keys.ToArray(); }
    }

}

i want to create 2 list, 1 list for the key and the other list contain the values of the selected keys, i tried this but it doesnt work 我想创建2个列表,其中1个列表用于键,而其他列表包含所选键的值,我尝试了此操作,但它不起作用

<ListBox x:Name="TagNameList" ItemsSource="{Binding Keys}"/>
<ListBox ItemsSource="{Binding Items[{Binding SelectedItem,ElementName=TagNameList}]}"/>

i contained them to a grid and their datacontext is an object of TAGs, the 1st listbox is working fine as its simple binding to a property but the 2nd list, isnt working any help? 我将它们包含到网格中,并且它们的datacontext是TAG的对象,第一个列表框可以正常工作,因为它简单地绑定到属性,但是第二个列表框却没有任何帮助?

"{Binding Items[{Binding SelectedItem,ElementName=TagNameList}]}" isn't valid xaml. "{Binding Items[{Binding SelectedItem,ElementName=TagNameList}]}"无效。 You can't do a binding inside a path since the path is constructed at compile time. 您不能在路径内进行绑定,因为该路径是在编译时构造的。

When encountering this myself I've simply ended up binding something else to the SelectedItem of one ListBox and then when that gets notified of a change, also fire a NotifyPropertyChanged on another property exposing Items[selectedKey] to which my second ListBox is bound. 当遇到这个问题时,我只是简单地将其他东西绑定到一个ListBoxSelectedItem上,然后在收到更改通知时,在另一个属性上激发一个NotifyPropertyChanged ,该对象暴露了我的第二个ListBox绑定到的Items[selectedKey]

xaml: xaml:

<ListBox x:Name="TagNameList" ItemsSource="{Binding Keys}" SelectedItem={Binding Selected}/>
<ListBox ItemsSource="{Binding ValueFromKey}"/>

C#: C#:

string _selected;
public string Selected
{
    get { return _selected; }
    set 
    { 
        _selected= value; 
        OnPropertyChanged("Selected");
        OnPropertyChanged("ValueFromKey");
    }
}

public string[] ValueFromKey
{
    get { return Items[Selected]; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM