我正在使用以下代码在 a 中显示 XML 文件的内容,ListBox
并在两个TextBox
es 中显示所选内容。
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="XML_View_Edit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="500"
Height="200">
<Window.Resources>
<XmlDataProvider x:Key="InventoryData"
XPath="Inventory/Books"
Source="Data.xml"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition
Height="100" />
<RowDefinition />
</Grid.RowDefinitions>
<ListBox
Grid.Row="0"
Name="listBox1" >
<ListBox.ItemsSource>
<Binding Source="{StaticResource InventoryData}" XPath="Book"/>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text = "{Binding XPath=Title}" />
<TextBlock Text= " - " />
<TextBlock Text = "{Binding XPath=Summary}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid
Grid.Row="1"
DataContext="{Binding ElementName=listBox1, Path=SelectedItem}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="70" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label
Content="Title"
Grid.Row="0"
Grid.Column="0"
Name="label_title" />
<Label
Content="Summary"
Grid.Row="1"
Grid.Column="0"
Name="label_summary" />
<TextBox
Grid.Column="1"
Name="textbox_title"
Text = "{Binding XPath=Title}"
Grid.Row="0" />
<TextBox
Grid.Column="1"
Name="textbox_summary"
Text = "{Binding XPath=Summary}"
Grid.Row="1" />
</Grid>
</Grid>
</Window>
用户可以更改TextBox
es 中的文本,这也与ListBox
. 但是如何保存/同步 XML 文件中的更改/与 XML 文件同步?