使用wpf / databinding / xaml在datagrid上的collectionview中仅显示1个项目的出现 [英] display only 1 occurence of item in collectionview on datagrid using wpf/databinding/xaml

查看:61
本文介绍了使用wpf / databinding / xaml在datagrid上的collectionview中仅显示1个项目的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,



我是wpf的新手,我需要一些绝望的帮助。老实说,我的搜索时间比我最近睡的时间多。



到目前为止,我在一个xml文件中读到了一个可观察的集合。然后在xaml中创建一个引用此可观察集合的CollectionViewSource。然后将datagrid数据绑定到collectionviewsource的属性。所有这一切都正常。



现在我想添加一些显示器的自定义。比方说,我的CVS中有3个名字。

1.GOLD

2.GOLD

3.SILVER



什么我想要的只是在我的网格中显示两个项目... GOLD和SILVER。每次只需1次。基本上和在SQL中说SELECT DISTINCT一样。



到目前为止,我使用multibinding / multiconverted来引入值...但我被困在这里。 />
实际上我甚至都不知道我是否正确的方式。



有人可以指点我正确的方向吗?如何在列表中只显示1个名称?



这是我的xaml ...

Good Evening All,

I am new to wpf and I am in need of some desperate assistance. I can honestly say I've put more hours searching than I've slept lately.

So far I read in an xml file to an observable collection. Then create a CollectionViewSource in xaml referencing this observable collection. Then databind a datagrid to the properties of the collectionviewsource. All this works correctly.

Now I want to add some customization of the display. Say for example I have 3 names in my CVS.
1.GOLD
2.GOLD
3.SILVER

What I want is just to show two items in my grid... GOLD and SILVER. Just 1 time each. Basically the same as saying SELECT DISTINCT in SQL.

So far I am using multibinding/multiconverted to bring in the values... but I am stuck here.
In fact I don't even know if I am going about it the correct way.

Can someone please point me in the right direction? How can I display only 1 occurrence of the names in my list?

Here is my xaml...

Window.Resources>
    <local:DataBindingClass x:Key="formatter"/>
    <local:MyDataSource x:Key="MyData"/>

    <CollectionViewSource x:Key="ViewSource" Source="{Binding Source={StaticResource ResourceKey=MyData}, Path=TradesClass}"/>
</Window.Resources

> <window.resources>
        <local:databindingclass x:key="formatter" xmlns:x="#unknown" xmlns:local="#unknown" />
        <local:mydatasource x:key="MyData" xmlns:x="#unknown" xmlns:local="#unknown" />
        
        <collectionviewsource x:key="ViewSource" source="{Binding Source={StaticResource ResourceKey=MyData}, Path=TradesClass}" xmlns:x="#unknown" />
    </window.resources>

<pre>
                    <DataGrid 

						x:Name="DataGridBreakdown" 

						Margin="10" 

						MaxHeight="300" 

						VerticalAlignment="Top"

						CanUserAddRows="False"

						AutoGenerateColumns="False"

						HorizontalContentAlignment="Stretch"

                        ItemsSource="{Binding Source={StaticResource ViewSource}}"

                            >


                        
                        <DataGrid.Resources>
                            <local:BreakdownGridValueConverter x:Key="myConverter"/>
                        </DataGrid.Resources>
                        
                        <!--<DataGrid.DataContext>
                            <local:TradesClass/>
                        </DataGrid.DataContext>-->

                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Market">
                                <DataGridTextColumn.Binding>
                                    <MultiBinding Converter="{StaticResource ResourceKey=myConverter}">
                                        <!--<Binding Path="."></Binding>-->
                                        <Binding Path="tMarketName"/>
                                        <Binding Source="{StaticResource ResourceKey=MyData}"/>
                                    </MultiBinding>
                                </DataGridTextColumn.Binding>
                            </DataGridTextColumn>
                                                
                                                

                            <DataGridTextColumn Header="Delta" Binding="{Binding tDeltaLive}"/>
                            <DataGridTextColumn Header="Prev Pos." Binding="{Binding Path=tDeltaLive}" />
                        </DataGrid.Columns>

                    </DataGrid>

推荐答案

好的我会尝试回答你的问题:



您需要在CollectionViewSource中使用过滤器。有关详细信息,请参阅此 MSDN Link。 [ ^ ]



您可以试试这个 StackOverFlow答案。 [ ^ ]



另一个。 [ ^ ]



希望这有帮助。
Ok I will try to answer your question:

You need to use the filter in CollectionViewSource. For more info refer this MSDN Link.[^]

You can try this StackOverFlow Answer.[^]

Another One.[^]

Hope this helps.


这篇关于使用wpf / databinding / xaml在datagrid上的collectionview中仅显示1个项目的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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