【问题标题】:DataBinding to WP8 Toolkit ExpanderView数据绑定到 WP8 Toolkit ExpanderView
【发布时间】:2013-06-16 23:10:52
【问题描述】:

我正在尝试使用以下 XAML 和 C# 类将数据绑定到 Windows Phone 8 Toolkit Expander 视图。我知道 DataContext 设置正确,因为标题具有正确的文本。但是,其余项目设置不正确(ExpanderTemplate 除外)

<phone:PanoramaItem Header="Skill Sheet">
    <ListBox Name="SkillSheet" ItemsSource="{Binding}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <toolkit:ExpanderView Header="{Binding}"
                                        ItemsSource="{Binding}"
                                        IsNonExpandable="False">
                    <toolkit:ExpanderView.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding groupName}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" LineHeight="{StaticResource LongListSelectorGroupHeaderFontSize}" />
                        </DataTemplate>
                    </toolkit:ExpanderView.HeaderTemplate>

                    <toolkit:ExpanderView.ExpanderTemplate>
                        <DataTemplate>
                            <TextBlock Text="Test" />
                        </DataTemplate>
                    </toolkit:ExpanderView.ExpanderTemplate>


                    <!--This is the area that is not getting databound-->
                    <toolkit:ExpanderView.ItemTemplate>
                        <DataTemplate>
                            <ListBox ItemsSource="{Binding skillNames}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding skill}" />
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </DataTemplate>
                    </toolkit:ExpanderView.ItemTemplate>
                </toolkit:ExpanderView>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</phone:PanoramaItem>

以下是 XAML 绑定到的类:

public class TreeMapSkill
{
    public string skill { get; set; }
}

public class TreeMapping
{
    public string groupName { get; set; }

    public List<TreeMapSkill> skillNames { get; set; }

    public TreeMapping()
    {
        skillNames = new List<TreeMapSkill>();
    }
}

public class TreeMappingList
{
    public List<TreeMapping> mapping { get; set; }

    public TreeMappingList() { }

    public TreeMappingList(Dictionary<string, List<string>> map)
        : base()
    {
        this.mapping = new List<TreeMapping>();

        foreach (string key in map.Keys)
        {
            TreeMapping tMap = new TreeMapping();
            tMap.groupName = key;
            foreach (string val in map[key])
                tMap.skillNames.Add(new TreeMapSkill() { skill = val });
            this.mapping.Add(tMap);
        }
    }

构造函数中的字典只是与特定组关联的技能列表。如果需要额外参考,我也可以提供一个示例对象。

【问题讨论】:

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


    【解决方案1】:

    为什么要在 Expander 的 ItemTemplate 中添加 ListBox?它已经是一个控件集合,因此您不需要其中的ListBox。只需将您的DataTemplate 放入其中即可。

    <toolkit:ExpanderView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding skill}" />
        </DataTemplate>
    </toolkit:ExpanderView.ItemTemplate>
    

    第二件事是你需要在扩展器的ItemSource属性的绑定上指定属性路径。

    <toolkit:ExpanderView Header="{Binding}"
                          ItemsSource="{Binding skillNames}"
                          IsNonExpandable="False">
    

    【讨论】:

    • 谢谢,这正是我需要的。
    猜你喜欢
    • 2023-03-16
    • 2011-03-14
    • 2014-03-29
    • 1970-01-01
    • 2011-01-08
    • 2014-12-09
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多