WPF 如何在具有 XML 元素或属性的文本框上设置正确的数据绑定 (XPath)?

WPF How do I setup proper data binding (XPath) on a textbox with an XML element or attribute?

美好的一天, 几天来我一直试图破解这个问题,但无济于事。 我有一个 xml 文件,我试图以动态方式将它的元素和属性绑定到各种文本框控件。我尝试过不同的方法来解决这个问题,但都失败了。 最后一次尝试是

<UserControl x:Class="L5RCharacterManager.CharacterHeaderControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:L5RCharacterManager"
             mc:Ignorable="d"
             d:DesignHeight="100" d:DesignWidth="800">

    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
            <Grid.Resources>
                <XmlDataProvider x:Key="data" Source="myXmlFile"/>
            </Grid.Resources>
            <Label Content="Clan" Grid.Column="0" Grid.Row="0" />
            <TextBox Grid.Column="1" Grid.Row="0">
                <TextBox.Text>
                    <Binding Source="{}" XPath="root/element/attribute" />
                </TextBox.Text>
            </TextBox>

谢谢你的时间。 编辑:我尝试绑定的 xml 文档的快速示例。

<?xml version="1.0" encoding="utf-8"?>
<root>
  <element1 attr11="Scorpion" attr12="" attr13="" attr14="" />
  <elements2>
    <element21 name="twentyone" value="21">
      <subElement1a name="twentyoneA" />
      <subElement2b name="twentyoneB" />
    </element21>
    <element22 name="twentytwo" value="22">
      <subElement1a name="twentytwoA" />
      <subElement2b name="twentytwoB" />
    </element22>
   </elements2>
</root>

示例:

        <StackPanel.Resources>
            <XmlDataProvider x:Key="xmlData" Source="myXmlFile.xml"/>
        </StackPanel.Resources>
        <TextBox Text="{Binding XPath=root/element1/@attr11, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                 Source={StaticResource xmlData}}"/>
        <TextBox Text="{Binding XPath=root/element1/@attr12, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                 Source={StaticResource xmlData}}"/>
    <StackPanel>
        <StackPanel.DataContext>
            <XmlDataProvider Source="myXmlFile.xml"/>
        </StackPanel.DataContext>
        <TextBox Text="{Binding XPath=root/element1/@attr11, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Text="{Binding XPath=root/element1/@attr12, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </StackPanel>

但编辑结果不会自动保存
为此,当您关闭 Window 时,获取 XmlDataProvider 并显式保存它 XML.

实现编辑结果保存的例子

    public static class Handlers
    {
        public static EventHandler SaveXmlOnClosed { get; } = (s, e) =>
        {
            Window window = (Window)s;

            XmlDataProvider provider = (XmlDataProvider)window.Resources["xmlData"];

            string file = provider.Source.OriginalString;

            provider.Document.Save(file);
        };
    }
<Window ----------------------
        ----------------------
        Closed="{x:Static local:Handlers.SaveXmlOnClosed}">
   <Window.Resources>
            <XmlDataProvider x:Key="xmlData" Source="myXmlFile.xml"/>
    </Window.Resources>
    <StackPanel>
        <TextBox Text="{Binding XPath=root/element1/@attr11, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                 Source={StaticResource xmlData}}"/>
        <TextBox Text="{Binding XPath=root/element1/@attr12, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                 Source={StaticResource xmlData}}"/>
    </StackPanel>
</Window>