Wpf 如何绑定到字典中的项目?

Wpf 如何绑定到字典中的项目?,wpf,xaml,data-binding,dictionary,datatemplate,Wpf,Xaml,Data Binding,Dictionary,Datatemplate,我正在用C#在WPF应用程序中使用Telerik的RadCarousel控件。RadCarousel类似于GridView,因为它绑定到一个集合,并显示集合中的每个项目(因此我的问题并不特定于Telerik或RadCarousel) 我使用DataTemplate来指定每个记录的显示方式 如果我这样做,它的工作很好: <DataTemplate> <TextBlock Text="{Binding Path=oMySubObject.TheAmount}" />

我正在用C#在WPF应用程序中使用Telerik的RadCarousel控件。RadCarousel类似于GridView,因为它绑定到一个集合,并显示集合中的每个项目(因此我的问题并不特定于Telerik或RadCarousel)

我使用DataTemplate来指定每个记录的显示方式

如果我这样做,它的工作很好:

<DataTemplate>
    <TextBlock Text="{Binding Path=oMySubObject.TheAmount}" />
</DataTemplate>

但是,如果我需要指向字典中的一个项目,该怎么办

<DataTemplate>
    <TextBlock Text="{Binding Path=myDictionaryOfSubObjects[TheCurrentIndex].TheAmount}" />
</DataTemplate>

这个我无法工作,也不知道怎么做。基本上…我需要在运行时指定索引,当它更新时,绑定会更新


有什么建议吗?

对于任何复杂的事情,你可能都应该做一个能干的人,并坚持下去


什么是“CURRENTINDEX”?

您只能在索引器中使用常量值,
将不会解析CURRENTINDEX
。有一些解决方法,比如将字典和索引传递给a来解析其中的项。

Window1.xaml

<Window x:Class="QuizBee.Host.Window1"
        x:Name="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ListView ItemsSource="{Binding ElementName=Window1, Path=myDictionary}" />
</Window>

Window1.xaml.cs

public partial class Window1:Window
{
    // the property must be public, and it must have a getter & setter
    public Dictionary<string, myClass> myDictionary { get; set; }

    public Window1()
    {
        // define the dictionary items in the constructor
        // do the defining BEFORE the InitializeComponent();

        myDictionary = new Dictionary<string, myClass>()
        {
            {"item 1", new myClass(1)},
            {"item 2", new myClass(2)},
            {"item 3", new myClass(3)},
            {"item 4", new myClass(4)},
            {"item 5", new myClass(5)},
        }; 

        InitializeComponent();
    }
}
公共部分类窗口1:窗口
{
//属性必须是公共的,并且必须具有getter和setter
公共词典myDictionary{get;set;}
公共窗口1()
{
//在构造函数中定义字典项
//在InitializeComponent()之前进行定义;
myDictionary=新字典()
{
{“项目1”,新myClass(1)},
{“项目2”,新myClass(2)},
{“项目3”,新myClass(3)},
{“项目4”,新myClass(4)},
{“项目5”,新的myClass(5)},
}; 
初始化组件();
}
}

//大概是这样的:

[ValueConversion(typeof(字典)、typeof(字符串))]
公共类字典转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
Dictionary dict=作为Dictionary的值;
返回dict[CurrentIndex].TheAmount;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回0;
}
}

…@Nullqwerty您可能还想尝试直接绑定到字典。值,不确定它是否有效,但值得一试。我正在寻找类似的内容。所以,这里是最重要的…如果我有20个文本块,每个都指向不同的属性,而不是“TheAmount”呢。我需要为每一个设置不同的转换器吗?@Nullqwerty您可以通过IValueConverter传递整个控件。因此,您可以将控件命名为“AmountControl”,然后将其通过转换器,然后获取datacontext以获取dict,然后您就有了一些逻辑,比如“if name is AmountControl”返回。TheAmount@Nullqwerty谢谢currentindex只是一个指定索引的属性。我能做到这一点,我已经把它作为一种解决办法。由于其他各种原因,它不适合我的库。我希望绑定能够处理它。这就是我试图找到的解决方案。不过还不够。
<Window.Resources>
  <NAMESPACEWHERECONVERTERRESIDES:DictionaryConverter x:Key="cDictionaryConverter"/>
</WindowResources>


    <TextBlock Text="{Binding Path=myDictionaryOfSubObjects, Converter={StaticResource cDictionaryConverter}}"/>