如果不清楚,请更正我的问题。我正在寻找的是......
这是一个字典的示例绑定......它有效:
<TextBlock Text="{Binding Path=MyDictionary[ThisIsKeyInMyDict]}" />
我正在寻找:
<TextBlock x:Name="Id" Text="{Binding Path=MyDictionary[x:Name]}" />
你知道吗?我想在字典中查找一个键,与#34; Name&#34;相同。这个控制。
感谢您的帮助!
答案 0 :(得分:2)
您可以使用MultiBinding:
<Window.Resources>
<local:DictValueConverter x:Key="dictValCnv"/>
</Window.Resources>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource dictValCnv}">
<Binding Path="MyDictionary"/>
<Binding RelativeSource="{RelativeSource Self}" Path="Name"/>
</MultiBinding>
</TextBlock.Text>
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
public class DictValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values==null || values.Length<2 )
{
return false;
}
var dict = values[0] as IDictionary;
if(dict.Contains(values[1]))
{
return dict[values[1]];
}
return "KeyNotFound";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}