我在 C# 的 WPF 应用程序中使用来自 Telerik 的 RadCarousel 控件。 RadCarousel 与 GridView 类似,因为它绑定(bind)到集合并显示集合中的每个项目(因此我的问题并非特定于 Telerik 或 RadCarousel)。
我使用 DataTemplate 来指定每条记录的显示方式。
如果我这样做,效果很好:
<DataTemplate>
<TextBlock Text="{Binding Path=oMySubObject.TheAmount}" />
</DataTemplate>
但是如果我需要指向字典中的某个项目怎么办?
<DataTemplate>
<TextBlock Text="{Binding Path=myDictionaryOfSubObjects[TheCurrentIndex].TheAmount}" />
</DataTemplate>
这我无法工作,也不知道如何。基本上...我需要在运行时指定索引,当索引更新时,绑定(bind)也会更新。
有什么建议吗?
最佳答案
<Window.Resources>
<NAMESPACEWHERECONVERTERRESIDES:DictionaryConverter x:Key="cDictionaryConverter"/>
</WindowResources>
<TextBlock Text="{Binding Path=myDictionaryOfSubObjects, Converter={StaticResource cDictionaryConverter}}"/>
//类似这样的:
[ValueConversion(typeof(Dictionary), typeof(string))]
public class DictionaryConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Dictionary<type, type> dict = value as Dictionary<type, type>;
return dict[CurrentIndex].TheAmount;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return 0;
}
}
关于wpf - 如何绑定(bind)到字典中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9184627/