将文本框文本绑定到wpf中的xml文件
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉没有给您提供完整的示例,但是这个 CodeProject< /a> 已经有一个教程项目完全可以满足您的需求
Sorry for not giving you a full example, but this CodeProject already has a tutorial project that does exactly what you want