【问题标题】:wpf combobox data binding xmlwpf组合框数据绑定xml
【发布时间】:2012-12-26 10:55:10
【问题描述】:

我想在我的组合框中显示所有组名 (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 时,会显示所有组名(但有双精度值,因为没有以这种方式分组)

组合框中的预期输出:

  • 组1
  • 组2

现在我有了(使用 TeleView):

  • 组1

如果我使用 Teledata:

  • 组1
  • 组1
  • 组2

【问题讨论】:

  • 尝试使用/ 而不是_
  • 这不是问题,这里写的时候打错了,我改了
  • contact_grname Iment :-)
  • 你期望得到的输出是什么?

标签: c# wpf binding combobox


【解决方案1】:

如果您只想显示Groups,您实际上可以通过ItemSource 路径访问CollectionViewSource.View.Groups 属性

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

输出:

  • 组1
  • 组2

【讨论】:

  • 谢谢!像魅力一样工作!但是当我使用 combobox.SelectedValue.ToString() 时,它显示 MS.Internal.Data.CollectionView.... 而不是选定的字符串:/有什么想法吗? SelectedItem.ToString() 结果相同
  • 您必须添加SelectedValuePath,这样您才能从SelectedValue 获得正确的值,我已经更新了答案:)
  • 非常感谢 sa_ddam213 :D 工作得很好!你也可以看看我的另一个帖子吗? stackoverflow.com/questions/14052468/wpf-filter-with-combox-xml 谢谢!
【解决方案2】:

这不是您问题的答案,但它可能会有所帮助。考虑一下这个组合框:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2015-12-12
    • 2014-02-03
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多