简体   繁体   English

wpf combobox数据绑定xml

[英]wpf combobox data binding xml

I want to display all groupnames (contact_grname) into my combobox, but only one item is showing up ! 我想在我的组合框中显示所有组名(contact_grname),但是只显示一项! Why is that ?! 这是为什么 ?!

<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}" />

my xml file: 我的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>

Just group1 is showing up (one time). 仅group1正在显示(一次)。 When I use StaticResource TeleData instead of TeleView in combobox, then all groupnames are displayed (but there are doubles values because not grouped that way) 当我在组合框中使用StaticResource TeleData而不是TeleView时,将显示所有组名(但存在双精度值,因为未采用这种方式分组)

Expected output in combobox: 组合框中的预期输出:

  • group1 组1
  • group2 组2

Now I have (with TeleView): 现在我有了(使用TeleView):

  • group1 组1

and if I use Teledata: 如果我使用Teledata:

  • group1 组1
  • group1 组1
  • group2 组2

This is not the answer to your question, but it might help. 这不是您问题的答案,但可能会有所帮助。 Consider this ComboBox: 考虑以下组合框:

<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>

It shows "Bart", "Eric" and "Mike". 它显示“ Bart”,“ Eric”和“ Mike”。 With this binding: 通过此绑定:

<Binding Source="{StaticResource Data}"
                     XPath="contact/contact_grname"  />

You will get "group1","group2". 您将获得“ group1”,“ group2”。 But this is because I changed xml. 但这是因为我更改了xml。 With your xlm, I mean with this: 对于您的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> 

the result is "group1","group1" and "group2". 结果是“ group1”,“ group1”和“ group2”。

So, One suggestion is to remove /contact from this line of code: 因此,一个建议是从以下代码行中删除/contact

<XmlDataProvider x:Key="TeleData" XPath="/response/contacts/contact">

If you just want to display the Groups you can actually access the CollectionViewSource.View.Groups property via the ItemSource path 如果只想显示Groups ,则可以通过ItemSource路径实际访问CollectionViewSource.View.Groups属性。

<ComboBox ItemsSource="{Binding Source={StaticResource TeleView}, Path=Groups}" SelectedValuePath="Name" />

Output: 输出:

  • group1 组1
  • group2 组2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM