WPF的ListView / GridView的允许用户选择多个项目,并将它们组合在一起 [英] WPF ListView/Gridview allow users to select multiple items and group them together

查看:661
本文介绍了WPF的ListView / GridView的允许用户选择多个项目,并将它们组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF的ListView / GridViwe在MVVM应用程序。 GridView控件绑定到视图模型列表。

I have a WPF ListView/GridViwe in a MVVM application. GridView is bound to a List in the ViewModel.

的要求是,用户应该能够选择GridView控件的多行,在其上单击右键,然后看到一个上下文菜单集团这些结合在一起。一旦选定,所有这些项目应该被折叠成一组用扩张器或+号开头补充说。

The requirement is that the users should be able to select multiple rows of the gridview, right-click on it and see a context menu "Group These Together". Once selected, all these items should be collapsed into one group with a expander or + sign added at the beginning.

有人可以帮我在得到这个工作?

Can somebody please help me in getting this working?

推荐答案

我在做类似的问题,但我从他们假设名字不得不组项目。我所做的只是创建 DataTrigger MultiDataTrigger 取决于您的数据的要求,然后当条件为真,即项目选择更改 GroupItem 的容器。我的意思是,当你创建你的列表视图,您必须对分组视图,这顺便说一句不归,你可以声明它没有扩展,只使用<创建code>的StackPanel 。之后,你需要3线code的设置分组。下面是一个例子:
MAIN.xaml

I was working on similar problem, but I had to group items by their let's say name. What I've done was create DataTrigger or MultiDataTrigger depending on your data requirements and then when conditions are true i.e. item selected change the container for GroupItem. What I mean is when you create your list view, you have to create it with grouped view, which btw is not grouped as you can declare it without expander and just use the StackPanel. After that you need 3 lines of Code to set the grouping. Here is an example for you: MAIN.xaml

<ListView 
                ScrollViewer.CanContentScroll="False" 
                x:Name="lsvProducts" 
                ItemsSource="{Binding Products, NotifyOnSourceUpdated=True}" 
                MouseDown="lsvProducts_MouseDown">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="Auto" Header="Code" DisplayMemberBinding="{Binding ID}"/>
                        <GridViewColumn Width="Auto" Header="Description" DisplayMemberBinding="{Binding Desc}"></GridViewColumn>
                        <GridViewColumn Width="Auto" Header="Qty" DisplayMemberBinding="{Binding Qty}"></GridViewColumn>
              </GridView>
                </ListView.View>
                <ListView.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource GroupedView}"/>
                </ListView.GroupStyle>
            </ListView>

正如你可以看到我已经宣布了分组式的一个空的容器,振振有辞,就是因为你不能没有previous声明分配给它。在此声明之后,你需要这个在你的 generic.xaml

<Style x:Key="GroupedView" TargetType="{x:Type GroupItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=chbx, Path=IsChecked}" Value="True">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander IsExpanded="False">
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                        <TextBlock Text="{Binding ItemCount}" FontSize="16" Foreground="DimGray" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                        <TextBlock Text=" item(s)" FontSize="16" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>

在你宣布你的,你需要声明一个真正的价值风格,而不扩展

After you declared your style for the true value you need to declare one for without expander

<!--This goes in the same style as the prevoius sample code -->
    <DataTrigger Binding="{Binding ElementName=chbx, Path=IsChecked}" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <StackPanel>
                                    <ItemsPresenter />
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>

下一步是定义分组:

Next step is to define the grouping:

//lsvProducts is our ListView
view = (CollectionView) CollectionViewSource.GetDefaultView(lsvProducts.ItemsSource);
//in the creation parameter you should put field that you want to group by :-)
PropertyGroupDescription grouping = new PropertyGroupDescription("FieldToGroupBy");
view.GroupDescriptions.Add(grouping);

,应该在视图模型冲击片雷管。
祝你好运!
让我们知道,如果你需要更多的帮助,我会尽我所能与我的英语; - )

Which should apper in your ViewModel. Good luck! Let us know if you need any more help, I'll try my best with my English ;-)

修改

我忘了,当你分配你需要检查已经在收集组的数量,只有当非或0应用它的分组提,否则你将应用分组多次和扩展将repeeated在多次添加的分组结果窗口: - )

I forgot to mention when you assign the grouping you need to check the number of groups already in the collection and only apply it when there is non or 0, otherwise you'll apply the grouping multiple times and the expander will be repeeated in the result window as many times as you added the grouping :-)

这篇关于WPF的ListView / GridView的允许用户选择多个项目,并将它们组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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