将文本框文本绑定到wpf中的xml文件

发布于 2025-01-02 03:42:02 字数 1001 浏览 2 评论 0原文

我是 WPF 编程新手。请在这里帮助我。

我希望在 xml 文件中更新文本框中输入的数据。这是我的代码。

xaml:

<Window.Resources>
    <XmlDataProvider x:Key="credential_data" Source="Credentials.xml" XPath="/Credentials/MB"/>
</Window.Resources>


     <StackPanel DataContext="{Binding Source={StaticResource credential_data}}">                            
              <TextBox Height="23" Name="textBox5" Width="188" Text="{Binding XPath=Hostname, Mode=TwoWay}"/>
              <TextBox Height="23" Name="textBox6" Width="188" Text="{Binding XPath=Port, Mode=TwoWay}"/>
    </StackPanel>

这是 Credentials.xml :

<?xml version="1.0" encoding="utf-8"?>
<Credentials>
    <MB>
        <Hostname>10.111.123.234</Hostname>
        <Port>8080</Port>
    </MB>
</Credentials>

现在发生的情况是 xml 文件中的任何更新都会反映在文本框中,但 textbox.text 中的任何更改不会更新 xml 文件。

我哪里出错了? :(

谢谢。

I am new to WPF programming. Please help me here.

I want the data entered in textboxes to be updated in the xml file. Here is my code.

xaml:

<Window.Resources>
    <XmlDataProvider x:Key="credential_data" Source="Credentials.xml" XPath="/Credentials/MB"/>
</Window.Resources>


     <StackPanel DataContext="{Binding Source={StaticResource credential_data}}">                            
              <TextBox Height="23" Name="textBox5" Width="188" Text="{Binding XPath=Hostname, Mode=TwoWay}"/>
              <TextBox Height="23" Name="textBox6" Width="188" Text="{Binding XPath=Port, Mode=TwoWay}"/>
    </StackPanel>

Here is Credentials.xml :

<?xml version="1.0" encoding="utf-8"?>
<Credentials>
    <MB>
        <Hostname>10.111.123.234</Hostname>
        <Port>8080</Port>
    </MB>
</Credentials>

What is happening now is that any updates in the xml file gets reflected in the textboxes but any changes in textbox.text does not update the xml file.

where am I going wrong?? :(

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2025-01-09 03:42:02

抱歉没有给您提供完整的示例,但是这个 CodeProject< /a> 已经有一个教程项目完全可以满足您的需求

Sorry for not giving you a full example, but this CodeProject already has a tutorial project that does exactly what you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文