6
我遇到了問題。我在我的WPF項目中使用TreeView
來可視化我的XML數據。問題是,當我編輯我的XmlDocument
時,它不會在TreeView
中刷新。但我注意到,當我檢查SelectedNode
時,它是我編輯和XmlNode
。所以我的「編輯」方法工作正常,但我的樹的視覺刷新只有一個問題。 .Refresh()
或.Items.Refresh()
也不起作用。WPF TreeView刷新
這裏是我的樹的模板:
<DataTemplate x:Key="AttributeTemplate">
<StackPanel Orientation="Horizontal"
Margin="3,0,0,0"
HorizontalAlignment="Center">
<TextBlock Text="{Binding Path=Name}"
Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="=""
Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="{Binding Path=Value, Mode=TwoWay}"
Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="""
Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Focusable="False">
<TextBlock x:Name="tbName" Text="?" FontFamily="Consolas" FontSize="8pt" />
<ItemsControl
ItemTemplate="{StaticResource AttributeTemplate}"
ItemsSource="{Binding Path=Attributes}"
HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<Binding XPath="*" />
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value, Mode=TwoWay}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
<Style x:Key="TreeViewAllExpandedStyle" TargetType="{x:Type TreeView}">
<Style.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
</Style.Resources>
</Style>
<Style x:Key="TreeViewAllCollapsedStyle" TargetType="{x:Type TreeView}">
<Style.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="False" />
</Style>
</Style.Resources>
</Style>
這裏有Window.Resources
:
<Window.Resources>
<XmlDataProvider x:Key="XmlData" />
</Window.Resources>
這裏是我的樹:
<TreeView x:Name="XmlTree" Grid.Row="1"
ItemsSource="{Binding Source={StaticResource XmlData}, XPath=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource NodeTemplate}"
SelectedItemChanged="XmlTree_SelectedItemChanged" />
這裏是我的代碼背後:
private XmlDocument _xml;
private XmlElement _selectedElement;
private XmlDataProvider _xmlDataProvider;
private void MainWindow_Load(object sender, EventArgs e)
{
XmlTree.Style = (Style)FindResource("TreeViewAllExpandedStyle");
_xmlDataProvider = FindResource("XmlData") as XmlDataProvider;
}
private void OpenXmlFile(string filePath)
{
_xml = new XmlDocument();
_xml.Load(filePath);
_xmlDataProvider.Document = _xml;
}
private void SaveChangesButton_Click(object sender, EventArgs e)
{
Dictionary<string, string> newAttributes = GetChangedAttributes();
foreach (KeyValuePair<string, string> pair in newAttributes)
{
_selectedElement.SetAttribute(pair.Key, pair.Value);
}
RefreshViews();
}
private void RefreshViews()
{
// now I don't know what to do here, any Refresh doesn't work:S
}
的第二件事是,如何清除我的樹,以便能夠再次用於其它數據(我有NullReferenceException
試圖XmlTree.Items.Clear();
男人你是最棒的!我需要這些神奇的線條: XmlTree.Items.Refresh(); XmlTree.UpdateLayout(); 非常感謝,解決了我的問題! – Alex 2013-05-03 08:54:22
Yeee,他們可以讓別人緊張:) – Nickon 2013-05-03 12:33:40