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>