如何绑定VB.NET WPF DataGrid和ListBox来显示XML内容? [英] How to bind VB.NET WPF DataGrid and ListBox to display XML content?

查看:130
本文介绍了如何绑定VB.NET WPF DataGrid和ListBox来显示XML内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件(参见下面的格式 - 只显示了两个条目),其中包含作者列表。

我想列出所有作者的ID(通常是'au_id'标签在XML中)在VB.NET WPF ListBox中并基于该ListBox中的选择,用一些或所有作者的剩余信息(例如au_lname,au_fname,phone)填充VB.NET WPF DataGrid。



I have a XML file (see format below - only two entries are shown) which contains a list of authors.
I'd like to list all authors' id (Typically 'au_id' tag in the XML) in a VB.NET WPF ListBox and based on the selection in that ListBox, populate a VB.NET WPF DataGrid with some or all authors' remaining information (e.g. au_lname, au_fname, phone).

<authors_table>
  <authors>
    <au_id>172-32-1176</au_id>
    <au_lname>White</au_lname>
    <au_fname>Johnson</au_fname>
    <phone>408 496-7223</phone>
    <address>10932 Bigge Rd.</address>
    <city>Menlo Park</city>
    <state>CA</state>
    <zip>94025</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>213-46-8915</au_id>
    <au_lname>Green</au_lname>
    <au_fname>Margie</au_fname>
    <phone>415 986-7020</phone>
    <address>309 63rd St. #411</address>
    <city>Oakland</city>
    <state>CA</state>
    <zip>94618</zip>
    <contract>true</contract>
  </authors>
...

The following code does not work: I can display the correct information in the ListBox but when I select one id in that ListBox, the DataGrid is never populated with the expected data:

<window x:class="MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<window.resources>
    <XmlDataProvider x:Key="XmlData" Source="E:\CMT\Automation\Testarea\Authors.xml"/>
</window.resources>
<grid>
    <listbox name="ListBox1" itemssource="{Binding Source={StaticResource XmlData}, XPath=Authors_Table/authors}" displaymemberpath="au_id" horizontalalignment="Left" height="202" margin="10,10,0,0" verticalalignment="Top" width="129" />
    <datagrid datacontext="{Binding ElementName=ListBox1, XPath=SelectedItem}" horizontalalignment="Left" margin="155,10,0,0" verticalalignment="Top" height="202" width="353">
        <datagrid.columns>
            <datagridtextcolumn x:name="FNameCol" binding="{Binding XPath=au_fname}" header="First Name" />
            <datagridtextcolumn x:name="LNameCol" binding="{Binding XPath=au_lname}" header="Last Name" />
            <datagridtextcolumn x:name="PhoneCol" binding="{Binding XPath=phone}" header="Phone" />
        </datagrid.columns>
    </datagrid>
</grid>


I obviously miss something in the way I bind the two WPF controls but I can't figure out what. If you have an idea, thanks in advance for your help.</window></authors_table>

推荐答案

亲爱的朋友,

我已经通过一个样本解释了你的问题。这是我演变过的一种独特的方法。请按照这篇文章。如果你不明白,请写信给我。



文章可在: -



http://www.filehosting.org/file/details/495815/PROJ-13.rar [ ^ ]



我非常抱歉对于>超过一年的延迟。实际上我从2015年2月开始我的wpf教程。这篇文章是在2015年7月左右上传的。通过这个。任何xml文件的原理都是一样的 - 无论它们多么复杂。



这将帮助您简化程序。


问候和祝福,

Venkatraman

(又名)kvinvisibleguy



查看所有我的文章



http://www.dreamincode.net/forums/topic/370955-wpf-helper/page__pid__2124851__st__0entry2124851







http://www.dreamincode.net/forums/topic/370955-wpf-helper/page__st__15__gopid__2129630entry2129630



http://www.dreamincode.net/forums/topic/370955-wpf-helper/page__st__30
Dear friend,
I have already explained by a sample,your problem.This is a unique method I have evolved.Do follow this article.If you do not understand,please write to me.

Article available at:-

http://www.filehosting.org/file/details/495815/PROJ-13.rar[^]

I am extremely sorry for the >than one year delay.Actually I started my wpf tutorials only from Feb 2015.This article was uploaded around July 2015.Go through this.The principle is the same for any xml file-however complicated they are.

This will help you to simplify the procedure very much.

Regards and Blessings,
Venkatraman
(aka)kvinvisibleguy

See all my articles at

http://www.dreamincode.net/forums/topic/370955-wpf-helper/page__pid__2124851__st__0entry2124851



http://www.dreamincode.net/forums/topic/370955-wpf-helper/page__st__15__gopid__2129630entry2129630

http://www.dreamincode.net/forums/topic/370955-wpf-helper/page__st__30


这篇关于如何绑定VB.NET WPF DataGrid和ListBox来显示XML内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆