【问题标题】:How Do You Bind To An Item In A Dictionary?您如何绑定到字典中的项目?
【发布时间】:2012-02-29 09:00:06
【问题描述】:

我在带有 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>

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

有什么建议吗?

【问题讨论】:

  • @Nullqwerty 您可能还想尝试直接绑定到 Dictionary.Values,不确定它是否有效,但值得一试。

标签: wpf xaml data-binding dictionary datatemplate


【解决方案1】:
<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;
    }
}

【讨论】:

  • 我一直在寻找这样的东西。所以,这里是踢球者......如果我实际上有 20 个文本块,每个文本块都指向“TheAmount”以外的不同属性。我需要一个不同的 Coverter 吗?
  • @Nullqwerty 您可以通过 IValueConverter 传递整个控件。因此,您可以将控件命名为“AmountControl”,然后将其传递给转换器,然后获取数据上下文以获取字典,然后您就有一些逻辑,例如“如果名称为 AmountControl”,则返回 .TheAmount
【解决方案2】:

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();
    }
}

【讨论】:

    【解决方案3】:

    您只能在索引器中使用常量值,TheCurrentIndex 将无法解析。有一些解决方法,例如将字典和索引传递给 multi value converter 以解决那里的项目。

    【讨论】:

    • 这就是我试图找到的解决方案类型。不过还不够。
    【解决方案4】:

    对于任何复杂的事情,您可能应该创建一个完成这项工作的 getter,并绑定到它。

    什么是“TheCurrentIndex”?

    【讨论】:

    • 谢谢。 TheCurrentIndex 只是一个指定索引的属性。我可以做到这一点,并且我已经将其作为一种解决方法。由于其他各种原因,它不适合我的图书馆。希望绑定能够处理它。
    猜你喜欢
    • 1970-01-01
    • 2011-02-28
    • 2015-12-25
    • 1970-01-01
    • 2012-09-18
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多