我在我的silverlight应用程序中有一个treeview控件,它使用2个HierarchicalDataTemplate以所需的格式显示数据。我想让这个树在第一次打开时自动展开(最好是一个代码片段,我可以随时调用它)。
对给定代码的任何替代方案也将受到欢迎。
<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类的可观察列表。完整的数据结构是as;
父元素是包含1到n个关键点对象的突发对象,任何给定的BurstPage都可以包含1到n个关键点对象。
BurstPage.Name (说1) Keypoint.Name (说A) Keypoint.Name (说B) Keypoint.Name (说C) BurstPage.Name (说2) BurstPage.Name (说3) Keypoint.Name (说D) Keypoint.Name (说E)
发布于 2011-07-06 19:48:31
我感觉你的帖子里少了很多代码……
但我认为您可能会发现以下内容很有用:one-more-platform-difference-more-or-less-tamed
它有一些很好的指针,告诉你如何绑定像treeview这样的控件,并在代码中使用它们。
发布于 2011-07-07 14:18:42
您说得对,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++;
}
}https://stackoverflow.com/questions/6594758
复制相似问题