问题描述
i我试图将xml树扩展到wpf的第一级,我使用附加属性。
i我面临当我第一次加载xml时出现问题,
这是第一次 uiElement.ItemContainerGenerator.ContainerFromItem() 返回null,但是在同一生命周期中,如果我尝试加载xml,这个属性工作正常。
TreeViewItem treeItem = uiElement .ItemContainerGenerator.ContainerFromItem(item)as TreeViewItem;
任何人都可以帮助我。
我尝试了什么:
下面这行是null,这个函数首次为null ContainerFromItem
TreeViewItem treeItem = uiElement.ItemContainerGenerator.ContainerFromItem(item)as TreeViewItem
private static v oid OnTreeviewPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
try
{
TreeView uiElement =(TreeView) d;
uiElement.UpdateLayout();
TreeViewItem ti = GetItemFromObject(uiElement);
foreach( uiElement.Items中的对象项)
$
TreeViewItem treeItem = uiElement.ItemContainerGenerator.ContainerFromItem(item)as TreeViewItem;
if(treeItem!= null)
{
// ExpandAll(treeItem, true);
treeItem.IsExpanded = true;
}
}
}
cat ch $>
{
}
}
查看:treeview在选项卡内
< ; window x:class =WpfApplication3.MainWindowxmlns:x =#unknown>
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =350Width = 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 =回收
Vm:ExpandXML.OpenXML ={Binding ISXmlExpand,UpdateSourceTrigger = PropertyChanged,NotifyOnSourceUpdated = True,Mode = TwoWay}>
< treeview.itemcontainerstyle> < Style TargetType ={x:Type TreeViewItem}>
< setter property =FontSizevalue =12>
< / Style>
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>
这篇关于Wpf treeview第一次没有正确扩展。如果在同一生命周期中再次加载,它正在工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!