[英]wpf combobox data binding xml
我想在我的組合框中顯示所有組名(contact_grname),但是只顯示一項! 這是為什么 ?!
<XmlDataProvider x:Key="TeleData" XPath="/response/contacts/contact">
</XmlDataProvider>
<CollectionViewSource x:Key="TeleView" Source="{StaticResource TeleData}" >
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="contact_name" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<dat:PropertyGroupDescription PropertyName="contact_grname" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<ComboBox ItemsSource="{Binding Source={StaticResource TeleView}, XPath=contact_grname}" />
我的xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
<contacts>
<contact>
<contact_grname>group1</contact_grname>
<contact_name>Bart</contact_name>
</contact>
<contact>
<contact_grname>group1</contact_grname>
<contact_name>Eric</contact_name>
</contact>
<contact>
<contact_grname>group2</contact_grname>
<contact_name>Mike</contact_name>
</contact>
</contacts>
</response>
僅group1正在顯示(一次)。 當我在組合框中使用StaticResource TeleData而不是TeleView時,將顯示所有組名(但存在雙精度值,因為未采用這種方式分組)
組合框中的預期輸出:
現在我有了(使用TeleView):
如果我使用Teledata:
這不是您問題的答案,但可能會有所幫助。 考慮以下組合框:
<ComboBox SelectedIndex="1">
<ComboBox.Resources>
<XmlDataProvider x:Key="Data"
XPath="response/contacts">
<x:XData>
<response>
<contacts>
<contact>
<contact_grname>group1</contact_grname>
<contact_name>Bart</contact_name>
<contact_name>Eric</contact_name>
</contact>
<contact>
<contact_grname>group2</contact_grname>
<contact_name>Mike</contact_name>
</contact>
</contacts>
</response>
</x:XData>
</XmlDataProvider>
</ComboBox.Resources>
<ComboBox.ItemsSource>
<Binding Source="{StaticResource Data}"
XPath="contact/contact_name" />
</ComboBox.ItemsSource>
</ComboBox>
它顯示“ Bart”,“ Eric”和“ Mike”。 通過此綁定:
<Binding Source="{StaticResource Data}"
XPath="contact/contact_grname" />
您將獲得“ group1”,“ group2”。 但這是因為我更改了xml。 對於您的xlm,我的意思是:
<contact>
<contact_grname>group1</contact_grname>
<contact_name>Bart</contact_name>
</contact>
<contact>
<contact_grname>group1</contact_grname>
<contact_name>Mike</contact_name>
</contact>
結果是“ group1”,“ group1”和“ group2”。
因此,一個建議是從以下代碼行中刪除/contact
:
<XmlDataProvider x:Key="TeleData" XPath="/response/contacts/contact">
如果只想顯示Groups
,則可以通過ItemSource
路徑實際訪問CollectionViewSource.View.Groups
屬性。
<ComboBox ItemsSource="{Binding Source={StaticResource TeleView}, Path=Groups}" SelectedValuePath="Name" />
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.