我正在尝试基于某些集合构建一个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
属性永远不会以这种方式工作,但是我不知道如何进行这种类型的绑定,并且我在网上没有找到任何可以给我线索的东西。
将 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; }
}