【问题标题】:Binding a Pivot to a Dictionary将枢轴绑定到字典
【发布时间】:2013-12-24 12:56:13
【问题描述】:

我正在尝试基于一些集合构建一个Pivot 系统,而不必使用代码隐藏来构建它。

我的集合是Dictionary<CategoriesEnum, List<object>>,我想将PivotItem 的标头绑定到CategoriesEnum 对象,而其内容必须绑定到相关的List<objet>

实际上我已经能够绑定PivotItem 的标头,但我真的不能为List 做到这一点。 这是我当前的代码:

(XAML)

        <phone:Pivot x:Name="pivot"
                     ItemsSource="{Binding Categories}">
            <phone:Pivot.ItemTemplate>
                <DataTemplate>
                    <phone:PivotItem Header="{Binding}">
                        <ListBox ItemsSource="{Binding Path=Objects}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Name}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </phone:PivotItem>
                </DataTemplate>
            </phone:Pivot.ItemTemplate>
        </phone:Pivot>

(C#)

public List<Categories> Categories
    {
        get
        {
            return new List<Categories>(Dictionary.Keys);
        }
    }

    public List<object> Objects
    {
        get
        {
            return Dictionary[(Categories)pivot.SelectedItem];
        }
    }

我知道Objects 属性永远不会以这种方式工作,但我不知道如何进行这种类型的绑定,我也没有在网上找到任何线索。

【问题讨论】:

  • 在您进行绑定之前,列表是否已创建并填充了数据?是否列出了任何绑定错误?
  • 你可以使用转换器来获取数据
  • 无绑定错误,列表已满! @techloverr:我会尽快尝试,因为这听起来不错
  • @techloverr:我在ListBoxItemsSource 中添加了Converter,但从未调用过Converter
  • 既然是字典,我想你想绑定到'Value'属性。

标签: c# xaml binding windows-phone-8


【解决方案1】:

将 ItemsSource 绑定到字典会枚举 KeyValuePairs,您可以从中绑定到 Key 和 Value 属性。假设您的字典中的键是字符串,值是可枚举的(IList): (XAML)

    <phone:Pivot x:Name="pivot" ItemsSource="{Binding MyDictionary}">
        <phone:Pivot.ItemTemplate>
            <DataTemplate>
                <phone:PivotItem Header="{Binding Key}">
                    <ListBox ItemsSource="{Binding Value}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding MyName}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </phone:PivotItem>
            </DataTemplate>
        </phone:Pivot.ItemTemplate>
    </phone:Pivot>

(C#)

public IDictionary<string, IList<MyObject>> MyDictionary { get; set; }
public class MyObject 
{ 
     public string MyName { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    相关资源
    最近更新 更多