C# xml更改后,与DynamicSource XmlDataProvider的网格绑定未更新
单击“添加”按钮后,下面的代码正在C# xml更改后,与DynamicSource XmlDataProvider的网格绑定未更新,c#,.net,xml,wpf,xaml,C#,.net,Xml,Wpf,Xaml,单击“添加”按钮后,下面的代码正在testdata.xml中成功添加节点,但无法刷新grid1中的树。 xaml代码: <Window x:Class="XmlDataTree.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/w
testdata.xml
中成功添加节点,但无法刷新grid1
中的树。
xaml代码:
<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文件代码:
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();
}
}
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Xml;
命名空间XmlDataTree
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有void xmladdnode()
{
字符串文件名=@“testdata.xml”;
//创建XmlDocument的新实例
XmlDocument doc=新的XmlDocument();
//从文件加载
//doc.Load(文件名);
//XmlDocument doc3=新的XmlDocument();
doc.Load(文件名);
XmlNode root1=doc.DocumentElement;
//创建一个新属性。
XmlElement elem=doc.CreateElement(“节点”);
xmldattribute attr=doc.CreateAttribute(“名称”);
属性值=“6”;
元素属性追加(attr);
root1.InsertAfter(elem,root1.LastChild);
文件保存(文件名);
XmlDataProvider oProv=grid1.DataContext作为XmlDataProvider;
oProv.Refresh();
}
私有void AddNode(对象发送方,RoutedEventArgs e)
{
点击按钮=(按钮)发送者;
MessageBox.Show(“按钮的名称是:“+单击的.name”);
xmladdnode();
}
}
}
将对象“XmlDataProvider oProv”移动到全局空间,以便可以在AddNode方法中进行刷新。