【问题标题】:Silverlight 4 : TreeView/ HierarchicalDataTemplate/ AutoExpand IssueSilverlight 4:TreeView/ HierarchicalDataTemplate/ AutoExpand 问题
【发布时间】:2011-07-07 14:18:42
【问题描述】:

我的 silverlight 应用程序中有一个树视图控件,它使用 2 个 HierarchicalDataTemplate(s) 以所需格式显示数据。我希望在第一次打开时自动扩展这棵树(最好是我可以随时调用的代码 sn-p)。

我们也欢迎给定代码的任何替代方案。

<sdk:TreeView x:Name="tvPageManager" Style="{StaticResource PageManagerStyle}"                                       
                        ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto">
                        <sdk:TreeView.ItemTemplate>
                            <sdk:HierarchicalDataTemplate ItemsSource="{Binding KeyPoints, Mode=TwoWay}">
                                <StackPanel Orientation="Horizontal">
                                    <ToolTipService.ToolTip>
                                        <ToolTip Content="{Binding PageName}" Style="{StaticResource ToolTipStyle}"/>
                                    </ToolTipService.ToolTip>
                                    <Image x:Name="imgPageIcon" Source="{Binding PageIconImage}" Style="{StaticResource PageIconStyle}" Tag="{Binding BurstPageId, Mode=TwoWay}" />
                                    <TextBlock x:Name="tbkLiteralTextPage" Text="Page " Style="{StaticResource PageNameLiteralTextBlockStyle}" />
                                    <TextBox x:Name="tbPageName" Text="{Binding PageName, Mode=TwoWay}" Style="{StaticResource PageNameTextBoxStyle}" />
                                </StackPanel>
                                <sdk:HierarchicalDataTemplate.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image x:Name="imgKeypointIcon" Source="../Assets/Images/bullet_yellow.png" Style="{StaticResource KeypointIconStyle}"/>
                                            <TextBlock x:Name="tbkKeypointTitle" Text="{Binding Title, Mode=TwoWay}" Style="{StaticResource KeypointNameTextBlockStyle}"  />
                                            <StackPanel x:Name="spnlMoveImages" Orientation="Horizontal" HorizontalAlignment="Right" Width="30">
                                                <Image x:Name="imgMoveUp" Source="../Assets/Images/up_arrow.png" Style="{StaticResource MoveIconsStyle}" Tag="{Binding KeyPointId}"/>
                                                <Image x:Name="imgMoveDn" Source="../Assets/Images/down_arrow.png" Style="{StaticResource MoveIconsStyle}" Tag="{Binding KeyPointId}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </DataTemplate>
                                </sdk:HierarchicalDataTemplate.ItemTemplate>
                            </sdk:HierarchicalDataTemplate>
                        </sdk:TreeView.ItemTemplate>
                    </sdk:TreeView>

此控件绑定到 BurstPage 类的 Observable 列表。完整的数据结构如下;

父元素是包含 1 到 n 个 BurstPage 对象的 Burst 对象,任何给定的 BurstPage 都可能有 1 到 n 个 Keypoint 对象。

BurstPage.Name(比如 1) Keypoint.Name(比如 A) Keypoint.Name(比如 B) Keypoint.Name(比如 C) BurstPage.Name(比如 2) BurstPage.Name(比如 3) Keypoint.Name(比如 D) Keypoint.Name(比如 E)

【问题讨论】:

    标签: .net silverlight treeview hierarchicaldatatemplate


    【解决方案1】:

    我感觉您的帖子中缺少很多代码...

    但我认为您可能会发现以下有用:one-more-platform-difference-more-or-less-tamed

    它有一些很好的指导,告诉您如何绑定树视图等控件,并在代码中使用它们。

    【讨论】:

    • 感谢安德斯,我已经更新了 XAML。请立即查看。
    • 好的,我认为您绝对可以使用我发布的链接中的方法将 TreeViewItem 的 IsExpanded 属性绑定到绑定数据对象中的属性。您是否遵循 MVVM 模式?
    • 我找到了一个更简单的自动展开解决方案; &lt;UserControl.Resources&gt; &lt;Style TargetType="sdk:TreeViewItem"&gt; &lt;Setter Property="IsExpanded" Value="True" /&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt;
    • 当然,如果您只希望最初扩展树,这是一种简单的方法。如果我误解了您的需求,我很抱歉:-)
    【解决方案2】:

    你是对的,XAML 最初只会扩展树,同时添加我使用的新节点;

    private void ExpandNode()
    {
        if (branchSelector < 1)
            return;
    
        TreeViewItem item = null;
        int itemAtIndex = 0;
    
        //Update tree layout
        this.tvName.UpdateLayout();
    
        foreach (var branch in this.tvName.Items)
        {
            item = (this.tvName.GetContainerFromItem(this.tvName.Items[itemAtIndex]) as TreeViewItem);
            if (item != null && item.HasItems)
            {
                if ((branch as Model.BranchBusinessObject).Id== branchSelector && (!item.IsExpanded))
                    item.IsExpanded = true;
            }
            itemAtIndex++;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多