如何覆盖子面板中的父数据上下文
我想覆盖子面板(stackpanel)之一中的父(网格)数据上下文, 这样任何 stackpanel、xml 文件中的任何更新都会相应更新。
最初,我在 XmlDataProvider 中有 XPath="/Data/MB" 属性。因此,只有文本框 5 和 6 中的绑定成功。
最后我所做的如下所示。这很好用。
<Grid>
<Grid.DataContext>
<XmlDataProvider x:Name="Credentials" Source="Credentials.xml"/>
</Grid.DataContext>
<StackPanel>
<TextBox Height="23" Name="textBox5" Width="188" Text="{Binding XPath=/Credentials/MessageBroker/Hostname}" />
<TextBox Height="23" Name="textBox6" Width="188" Text="{Binding XPath=/Credentials/MessageBroker/Port}"/>
</StackPanel>
<StackPanel>
<TextBox Height="23" Name="textBox9" Width="188" Text="{Binding XPath=/Credentials/Database/Server}" />
<TextBox Height="23" Name="textBox10" Width="188" Text="{Binding XPath=/Credentials/Database/Password}"/>
</StackPanel>
</Grid>
这是 Credentials.xml
<?xml version="1.0" encoding="utf-8"?>
<Credentials>
<MB>
<Hostname>145.111.227.222</Hostname>
<Port>5672</Port>
<UserName>Admin</UserName>
<Password>Admin</Password>
</MB>
<Database>
<Server>145.111.227.234</Server>
<UserID>Administrator</UserID>
<Password>password</Password>
</Database>
有没有更好的方法呢?
请告诉我。
I want to override parent (grid) datacontext in one of the child panel (stackpanel),
such that any update in any of the stackpanel, xml file will get updated accordingly.
Initially I had XPath="/Data/MB" attribute in XmlDataProvider. So with this binding in only textbox 5 and 6 was successful.
Finally what I did is given below. This works fine.
<Grid>
<Grid.DataContext>
<XmlDataProvider x:Name="Credentials" Source="Credentials.xml"/>
</Grid.DataContext>
<StackPanel>
<TextBox Height="23" Name="textBox5" Width="188" Text="{Binding XPath=/Credentials/MessageBroker/Hostname}" />
<TextBox Height="23" Name="textBox6" Width="188" Text="{Binding XPath=/Credentials/MessageBroker/Port}"/>
</StackPanel>
<StackPanel>
<TextBox Height="23" Name="textBox9" Width="188" Text="{Binding XPath=/Credentials/Database/Server}" />
<TextBox Height="23" Name="textBox10" Width="188" Text="{Binding XPath=/Credentials/Database/Password}"/>
</StackPanel>
</Grid>
Here is Credentials.xml
<?xml version="1.0" encoding="utf-8"?>
<Credentials>
<MB>
<Hostname>145.111.227.222</Hostname>
<Port>5672</Port>
<UserName>Admin</UserName>
<Password>Admin</Password>
</MB>
<Database>
<Server>145.111.227.234</Server>
<UserID>Administrator</UserID>
<Password>password</Password>
</Database>
Is there any better way to do it?
Please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论