如何覆盖子面板中的父数据上下文

发布于 2025-01-03 14:11:22 字数 1507 浏览 1 评论 0原文

我想覆盖子面板(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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文