0
我有以下代碼來打開XML文件並將其綁定到TreeView,但是如何將子節點或父節點添加到選定節點?謝謝。WPF - 將節點添加到樹視圖並將其保存爲XML
<Window.Resources>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<HierarchicalDataTemplate.ItemsSource>
<Binding XPath="child::*" />
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<XmlDataProvider x:Key="xmlDataProvider"></XmlDataProvider>
</Window.Resources>
<Grid>
<TreeView Margin="0,24,0,143"
Name="treeView1"
Background="AliceBlue"
ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}"
ItemTemplate= "{StaticResource NodeTemplate}"/>
<DockPanel Height="59"
Name="dockPanel1"
VerticalAlignment="Bottom"
Background="AliceBlue"></DockPanel>
<DockPanel Height="23"
Name="dockPanel2"
VerticalAlignment="Top"
Background="AliceBlue">
<Button Height="23"
Name="button1"
Width="75"
Click="button1_Click">Open</Button>
</DockPanel>
</Grid>
Button1的事件:
Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
open.Filter = "XML Files (*.xml)|*.xml";
if (open.ShowDialog(this) == true)
{
XmlDocument x = new XmlDocument();
x.Load(open.FileName);
XmlDataProvider dataProvider = this.FindResource("xmlDataProvider") as XmlDataProvider;
dataProvider.Document = x;
}
你可以添加更多的信息!你想用button1.click事件做到這一點?什麼問題? – Rev 2011-05-15 07:53:06
我想要做的是將一個子節點添加到節點,或者在所選節點上向它添加一個父節點。 – user646265 2011-05-15 09:09:46
我不會使用XmlDataProvider。相反,我會創建一個clr對象的集合並將它們綁定到treeview。 – vorrtex 2011-05-15 11:01:28