如果我的问题不清楚,请纠正我的问题。我正在寻找的是..
这是字典的示例绑定(bind)......它有效:
<TextBlock Text="{Binding Path=MyDictionary[ThisIsKeyInMyDict]}" />
我正在寻找:
<TextBlock x:Name="Id" Text="{Binding Path=MyDictionary[x:Name]}" />
你看到了吗?我想在字典中查找一个键,与该控件的“名称”相同。
感谢您的帮助!
最佳答案
您可以使用多重绑定(bind):
<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();
}
}
关于c# - WPF 使用 x :Name as a key 绑定(bind)到字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42987402/