其他分享
首页 > 其他分享> > CodeGo.net>数据绑定到WP8工具包ExpanderView

CodeGo.net>数据绑定到WP8工具包ExpanderView

作者:互联网

我正在尝试使用以下XAML和C#类将数据绑定到Windows Phone 8 Toolkit Expander视图.我知道正确设置DataContext是因为Headers具有正确的文本.但是,其余各项设置不正确(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);
        }
    }

构造函数中的Dictionary只是与特定组相关的技能列表.如果需要其他参考,我还可以提供一个示例对象.

解决方法:

为什么要在扩展器的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">

标签:data-binding,windows-phone-8,c
来源: https://codeday.me/bug/20191123/2065439.html