开发者

Silverlight 4 : TreeView/ HierarchicalDataTemplate/ Get Selected Node/ Leaf Issue

<sdk:TreeView x:Name="tvPageManager" SelectedItemChanged="tvPageManager_SelectedItemChanged" 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}" MouseLeftButtonDown="imgPageIcon_MouseLeftButtonDown" 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}" TextChanged="tbPageName_TextChanged" />
                                </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}" MouseLeftButtonDown="imgMoveUp_MouseLeftButtonDown" Tag="{Binding KeyPointId}"/>
                                                <Image x:Name="imgMoveDn" Source="../Assets/Images/down_arrow.png" Style="{StaticResource MoveIconsStyle}" MouseLeftButtonDown="imgMoveDn_MouseLeftButtonDown" Tag="{Binding KeyPointId}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </DataTemplate>
                                </sdk:HierarchicalDataTemplate.ItemTemplate>
                            </sdk:HierarchicalDataTemplate>
                        </sdk:TreeView.ItemTemplate>
                    </sdk:TreeView>      

for above XAML, i need to know programatically, which is the selected node or leaf ?


You are already catching the selection event "tvPageManager_SelectedItemChanged" so the current node is passed as a parameter (e.NewValue):

private void tvPageManager_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<Object> e)
{
    //Perform actions when SelectedItem changes
    MessageBox.Show(((TreeViewItem) e.NewValue).Header.ToString());
}


HiTech Magic, I am sorry for not adequately explaining my question, i have found out the solution;

    private void ExpandNodeAndSelectChild()
    {
        if (branchSelector < 1 || leafSelector < 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;

                foreach (var leaf in item.Items)
                {
                    item = (this.tvName.GetContainerFromItem(leaf as Model.LeafBusinessObject) as TreeViewItem);
                    if (item != null && ((leaf as Model.LeafBusinessObject).Id== leafSelector))
                    {
                        item.IsSelected = true;
                        break;
                    }                        
                }
            }
            itemAtIndex++;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜