1

我正在使用以下代码在 a 中显示 XML 文件的内容,ListBox并在两个TextBoxes 中显示所选内容。

<?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>

用户可以更改TextBoxes 中的文本,这也与ListBox. 但是如何保存/同步 XML 文件中的更改/与 XML 文件同步?

4

2 回答 2

1

在事件处理程序(buttonClick 或 windowClose)中,您可以访问资源:

var provider = Resources["inventoryData"] as XmlDataProvider;

并且提供者有一个 Document 属性。我不确定你是否可以覆盖但类似:

provider.Document.Save(newFileName);

或者

provider.Document.Save(provider.Source.ToString());  // existing file name

应该管用。

于 2013-01-13T20:27:34.333 回答
0

遵守有关 XmlDataProvider 使用的专家建议

Q: So two way binding utilizing the XmlDataProvider only works between the target and the "in memory" XML? Is there a way without doing my own XML file serialization to get WPF to write out the in memory XML back to the hard disk? A: Correct. You have to do your own file serialization.

这和可以帮助你。

于 2013-01-13T20:15:36.090 回答