简体   繁体   English

WPF / XAML:在文本框中查看/编辑XML文件

[英]WPF/XAML: Viewing/editing XML file in a TextBox

I'm rather new to WPF so please bear with me. 我是WPF的新手,请耐心等待。 I'm trying to bind an XML file to a TextBox so I can view the content and edit the file. 我正在尝试将XML文件绑定到TextBox,以便可以查看内容并编辑文件。 I tried several suggestions already (SO, Google, ...) but I can't seem to get this working. 我已经尝试了一些建议(例如,SO,Google等),但似乎无法正常工作。

My XMLDataProvider looks like this: 我的XMLDataProvider看起来像这样:

<Window.Resources>

    <XmlDataProvider x:Key="test" Source="U:\x\2616004.xml" />

</Window.Resources>

My TextBox is within a TabItem and looks like this: 我的文本框在TabItem中,如下所示:

  <Grid Background="#FFE5E5E5">
       <TextBox x:Name="tbBiblio" Background="White" HorizontalAlignment="Left" Height="548" Margin="7,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="773" 
                                 Text="{Binding Source={StaticResource test}, XPath=/*/}" />
  </Grid>

If I run the application it just gives me an empty TextBox. 如果我运行该应用程序,它只会给我一个空的TextBox。 I also tried different XML files, TextBlock instead of a TextBox and so on. 我还尝试了其他XML文件,TextBlock而不是TextBox等。

Suggestions and help are much appreciated. 建议和帮助深表感谢。 Thanks! 谢谢!

Edit: 编辑:

With Philip Campbell's suggestion the content of the XML file is now correctly parsed into the TextBox. 有了Philip Campbell的建议,现在可以将XML文件的内容正确地解析到TextBox中。 How do I get the TextBox now to show the "raw" file with its tags? 我现在如何获取TextBox来显示带有标签的“原始”文件?

As Default suggests, if you look at the output window you'll see what the error is: 默认所建议的那样,如果您查看输出窗口,您将看到错误是什么:

XML binding failed. Cannot obtain result node collection because of bad source node or bad Path...

Which means that your XPath is invalid. 这意味着您的XPath无效。 I don't know what format your XML is in, or what elements you're trying to read, but changing the XPath to "*" should give you a good starting point, ie: 我不知道您的XML使用哪种格式,或者您想读取什么元素,但​​是将XPath更改为“ *”应该为您提供一个很好的起点,即:

{Binding Source={StaticResource test}, XPath=*}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM