c# - xml 更改后,与 DynamicResource XmlDataProvider 的网格绑定未更新
问题描述
以下代码testdata.xml
在单击添加按钮后成功添加节点,但无法刷新树grid1
。xml代码:
<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();
}
}
}
解决方案
推荐阅读
- javascript - “return render_template()”不起作用 [XMLHttpRequest() & flask]
- mysql - MySQL数据库导入大文件失败
- amazon-web-services - AWS CloudFormation 仅将 SNS 限制为特定的 SQS 队列
- flutter - Flutter 可滚动导航抽屉,其中一个菜单项与底部对齐
- reactjs - “未捕获的 SyntaxError:无法在模块外使用 import 语句”——这是什么意思?
- web - 我无法获得可以帮助我在本地主机上运行网站的命令
- node.js - 如何在使用 mocha 和 chai 进行单元测试时删除 mongodb 上的集合数据
- spring - @CreationTimestamp 不适用于即时变量
- visual-studio - Visual Studio 2019 中的列号和字符号之间是否存在差异?
- computer-science - 如何找出下面 NPDA 接受的语言