【问题标题】:WPF How do I setup proper data binding (XPath) on a textbox with an XML element or attribute?WPF 如何在带有 XML 元素或属性的文本框上设置正确的数据绑定 (XPath)?
【发布时间】:2021-06-18 22:10:47
【问题描述】:

美好的一天, 我已经尝试破解这几天了,但无济于事。 我有一个 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>

【问题讨论】:

  • 为了帮助您,需要您使用的 XML 文件内容的最小示例。
  • 我添加了一个简单的示例,对于可怕的命名约定感到抱歉。

标签: xml wpf xaml data-binding


【解决方案1】:

例子:

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

但编辑结果不会自动保存。
为此,当您关闭窗口时,获取 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>

【讨论】:

  • 好消息是跟随这里的内容,我得到了它的工作。大多数情况下......有一些错误,它不喜欢文件的相对路径,当我给它一个绝对路径时,provider.Document 会引发异常。 System.NullReferenceException:“对象引用未设置为对象的实例。”提供者为空。
  • 我还需要动态绑定 xml 数据库,以便更新文本框,反之亦然 OnPropertyChange 或 OnLostKeyboardFocus。但是使用DynanmicResource 失败,因为它告诉我它需要绑定到一个dependencyProperty,但我不知道如何添加它。我的意思是我尝试从 Textbox 继承并添加功能,但 WPF 不会让我这样做。说当我这样做时我正在尝试从多个类继承。
  • "动态绑定 xml 数据库,以便更新" - 我不清楚你的意思是什么。也许您的意思是,文件“myXmlFile.xml”的内容不仅可以通过提供者“xmlData”进行更改,还可以通过其他方式进行更改。并且您希望此提供程序根据“myXmlFile.xml”中的当前数据更改其状态?
  • 是的。
  • 这是不可能的,原因有很多。 1 - XML文件不是数据库。并且不可能更改其中的任何单独部分。要更改至少一个符号,您需要重写整个文件。覆盖它意味着删除旧文件并创建一个具有相同名称的新文件。 2 - Net 平台无法从文件系统接收有关文件操作的通知。因此,没有简单的方法可以知道文件何时会被覆盖并且您需要更新其视图。
猜你喜欢
  • 1970-01-01
  • 2011-09-03
  • 2010-09-30
  • 2012-06-23
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
相关资源
最近更新 更多