Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am trying to expand xml tree to first level in wpf, iam using attached property for this.

i am facing problem when i am loading xml fo r the first time at that time ,

here for first time " uiElement.ItemContainerGenerator.ContainerFromItem() " is returning null ,but on same life cycle if i try to load xml this property is working properly.


TreeViewItem treeItem = uiElement.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

can any one help me.

What I have tried:

this below line is giving null , this funtion is null ContainerFromItem for the first time

TreeViewItem treeItem = uiElement.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem


private static void OnTreeviewPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
try
{
TreeView uiElement = (TreeView)d;
uiElement.UpdateLayout();
TreeViewItem ti = GetItemFromObject(uiElement);

foreach (object item in uiElement.Items)
{

TreeViewItem treeItem = uiElement.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;


if (treeItem != null)
{
// ExpandAll(treeItem, true);
treeItem.IsExpanded = true;
}


}


}
catch
{

}


}


view: treeview is inside a tab

<window x:class="WpfApplication3.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<grid>
<tabcontrol>
<tabitem>
<treeview name="xmlTree"> HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemTemplate= "{StaticResource NodeTemplate}"
ItemsSource="{Binding xmldocumrnt, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Recycling"
Vm:ExpandXML.OpenXML="{Binding ISXmlExpand,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True,Mode=TwoWay}" >
<treeview.itemcontainerstyle> <Style TargetType="{x:Type TreeViewItem}">
<setter property="FontSize" value="12">

</Style>



Posted
Updated 28-Jul-16 3:18am
Comments
Sergey Alexandrovich Kryukov 4-Jun-16 23:09pm    
Please use the debugger and see where the line expanding the node is executed, check up if it happens at all or not.
You did not show where the function OnTreeviewPropertyChanged is called.

In other words, do appropriate debugging work. When and if you find out what's going on, in case you still don't see how to fix the code, provide more comprehensive information based on your observations. Ideally, you could write some simplified test code focusing only on one problem; then it can be made small enough to show 100% of code. Please see: SSCCE.

—SA

1 solution

XML
Hi Avinesh,

 
you can try to add Setter property="IsExpended" value="True".
please find below code as Reference.......  


<TreeView Name="TreeView" BorderThickness="0" Foreground="Black" >
                                <TreeView.ItemContainerStyle>
                                    <Style TargetType="{x:Type TreeViewItem}">
                                        <Setter Property="IsExpanded" Value="True" />
                                    </Style>
                                </TreeView.ItemContainerStyle>

Please vote if it helpful . 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900