Below code is successfully adding node in testdata.xml after clicking add button, but unable to refresh the tree in grid1.
xaml code:
<Window x:Class="XmlDataTree.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XmlData Tree Test"
Width="250"
Height="350" >
<Window.Resources>
<XmlDataProvider x:Key="xmldata"
Source="testdata.xml"
XPath="/root" />
<HierarchicalDataTemplate DataType="Node"
ItemsSource="{Binding XPath=./*}">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0"
Text="Node:" />
<TextBlock Margin="5,0,0,0"
Text="{Binding XPath=@name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="leaf">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0"
Text="Leaf:" />
<TextBlock Margin="5,0,0,0"
Text="{Binding XPath=@name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="root"
ItemsSource="{Binding XPath=./*}">
<TextBlock Margin="0"
Text="ROOT" />
</HierarchicalDataTemplate>
</Window.Resources>
<!--
This sets the DataContext of the Grid to the XmlProvider
with a key of xmldata (set above in the Window.Resources
section).
-->
<Grid Name="grid0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid DataContext="{DynamicResource xmldata}" Grid.Row="0" x:Name="grid1">
<!--
Setting the ItemsSource to "{Binding}" causes the TreeView
to use the Grid's DataContext as its source, and the
templates describe h ow each tag is to be displayed and
handled, in case the node has descendent nodes.
-->
<TreeView Name="dirTree"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding}"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard" />
</Grid>
<Grid Grid.Row="1">
<Button Name="Add" Grid.Row="1" Width="50" Height="20" Content="Add Button" HorizontalAlignment="Left" Click="AddNode"></Button>
<TextBox Height="23" Grid.Row="1" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="127" />
</Grid>
</Grid>
</Window>
cs file code:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml;
namespace XmlDataTree
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void xmladdnode()
{
string filename = @"testdata.xml";
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
//load from file
//doc.Load(filename);
//XmlDocument doc3 = new XmlDocument();
doc.Load(filename);
XmlNode root1 = doc.DocumentElement;
//Create a new attrtibute.
XmlElement elem = doc.CreateElement("Node");
XmlAttribute attr = doc.CreateAttribute("name");
attr.Value = "6";
elem.Attributes.Append(attr);
root1.InsertAfter(elem, root1.LastChild);
doc.Save(filename);
XmlDataProvider oProv = grid1.DataContext as XmlDataProvider;
oProv.Refresh();
}
private void AddNode(object sender, RoutedEventArgs e)
{
Button clicked = (Button)sender;
MessageBox.Show("Button's name is: " + clicked.Name);
xmladdnode();
}
}
}