0

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

给定代码的任何替代方案也将受到欢迎。

<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)

4

2 回答 2

0

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

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

它有一些关于如何绑定像树视图这样的控件并在代码中使用它们的很好的指导。

于 2011-07-06T11:48:30.710 回答
0

没错,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++;
    }
}
于 2011-07-07T06:18:42.260 回答