C# 字典的动态绑定路径组合框
在我的XAML中,我有一个ItemsControl的DataTemplate,其中我必须将combobox控件绑定到同一个Dictionary对象 但是我希望这些组合框显示不同的项,而这种差异必须由dictionary键提供 例如,如果我写:C# 字典的动态绑定路径组合框,c#,wpf,dictionary,combobox,C#,Wpf,Dictionary,Combobox,在我的XAML中,我有一个ItemsControl的DataTemplate,其中我必须将combobox控件绑定到同一个Dictionary对象 但是我希望这些组合框显示不同的项,而这种差异必须由dictionary键提供 例如,如果我写: <Combobox ItemsSource="{Binding Path=MyDictionary[Key]}"/> 然后,组合框将显示指定键给出的所有值。但是对于我的ItemsControl中的每个项,必须更改此“[key]”部分 有没有一
<Combobox ItemsSource="{Binding Path=MyDictionary[Key]}"/>
然后,组合框将显示指定键给出的所有值。但是对于我的ItemsControl中的每个项,必须更改此“[key]”部分
有没有一个解决方案,包括字符串连接之类的,谁能帮我实现呢?我找不到任何动态构建绑定路径值的内容
提前感谢。您可以将多重绑定与多重转换器结合使用来实现这一点 你的xaml
<MultiBinding Converter="{StaticResource MyConverter}">
<Binding Path="MyDictionary" />
<Binding Path="Key" />
</MultiBinding>
转换器可能看起来像这样
public class DictionaryConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var key = values[1] as YourKeyType;
var dictionary = values[0] as Dictionary<YourKeyType, YourValueType>;
return dictionary[key];
}
}