how to turn off (with a checkbox) WPF/MVVM DataGrid grouping that is implemented using CollectionViewSource?

char m

I have working implementation of datagrid grouping. Below is what i do (GroupStyle with expander omitted):

<CollectionViewSource x:Key="SelectedObjectsViewSource" Source="{Binding SelectedObjectItems}">
    <CollectionViewSource.GroupDescriptions>
       <PropertyGroupDescription PropertyName="TableId"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>


<DataGrid Name="SelectedObjectsGrid" 
     ItemsSource="{Binding Source={StaticResource SelectedObjectsViewSource}}"
     SelectionMode="Extended"
     CanUserAddRows="False"
     AutoGenerateColumns="False">

I would like to add a checkbox that user can turn off/on grouping. But i have no idea how to implement this in MVVM

Alessandro Rossi

I advise you to bind the checkbox to a bool property of the viewmodel where its setter also sets the grouping status of the collectionview following the assigned value. Like in the following sample: where the bool property bound to the checkbox status is GroupView and the collection bound to the datagrid is View.

C# ViewModel

class ViewModel : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private ICollectionView _View;
    public ICollectionView View
    {
        get
        {
            return _View;
        }
        set
        {
            _View = View;
            NotifyPropertyChanged("View");
        }
    }

    private bool _GroupView;
    public bool GroupView
    {
        get
        {
            return _GroupView;
        }
        set
        {
            if (value != _GroupView)
            {
                // Clear Grouping status of the view
                View.GroupDescriptions.Clear();
                if (value)
                {
                    // If true set Grouping status
                    View.GroupDescriptions.Add(new PropertyGroupDescription("TableId"));
                }
                _GroupView = value;
                NotifyPropertyChanged("GroupView");

                // Notify the UI that also the View changed in order to redraw the datagrid with or without grouping
                NotifyPropertyChanged("View");
            }
        }
    }   
}

C# Code Behind

public partial class MyWindow : Window
{
    public MyWindow()
    {
        InitializeComponent();
        ViewModel myViewModel = new ViewModel();
        myViewModel.View = .....;
        DataContext = myViewModel;

    }
}

XAML

<StackPanel>
    <CheckBox IsChecked="{Binding GroupView, Mode=TwoWay}"/>
    <DataGrid Name="SelectedObjectsGrid" 
            ItemsSource="{Binding View, Mode=TwoWay}"
            SelectionMode="Extended"
            CanUserAddRows="False"
            AutoGenerateColumns="False"/>
</StackPanel>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to turn off write protection in win 7 using diskpart?

분류에서Dev

How to turn off Wireless power management permanently

분류에서Dev

How to turn off screen (headless installation)

분류에서Dev

Is it possible to turn off case insensitivity using pattern only?

분류에서Dev

How do I turn off all the password prompts?

분류에서Dev

How to turn off one of SATA HDDs installed in the computer to save power?

분류에서Dev

How to turn off single user mode on a remote machine?

분류에서Dev

How to turn off the boot up beep of Ubuntu Live USB?

분류에서Dev

how to turn off password-less login for ssh?

분류에서Dev

How to turn off one of SATA HDDs installed in the computer to save power?

분류에서Dev

Turn off spellcheck in OneNote 2010

분류에서Dev

Checkbox with Catel EventToCommand not working in Datagrid

분류에서Dev

How to turn off auto start on-screen-keyboard on Windows 7 64 bit?

분류에서Dev

How can I get the Quill editor authorship module to turn highlighting on and off?

분류에서Dev

How can you turn Core Isolation Memory Integrity back off again in Windows 10 build 17127 and later

분류에서Dev

How do I turn off bullet list formatting all of the lines to bullet lists in Word 2013?

분류에서Dev

How do I turn off the annoying clicking sound when browsing web pages in Internet Explorer?

분류에서Dev

How to get unchecked checkbox using Flask? (Or something!)

분류에서Dev

Turn off the link hover statusbar in Google Chrome

분류에서Dev

Redirect APPCRASH Dumps (Or turn them off)

분류에서Dev

Windows 7 does not turn off monitor displays

분류에서Dev

Extension to turn off certain page element?

분류에서Dev

Turn off history for Durandal child router

분류에서Dev

Windows 10: Laptop display refuses to turn off

분류에서Dev

Android 5 turn off Interruptions filtration

분류에서Dev

Chrome - Turn off bell for find in page no results?

분류에서Dev

Turn off screen with password protection but no sleep

분류에서Dev

Turn off auth in ubuntu 19.10 in all components

분류에서Dev

Turn off smooth scrolling in GTK3

Related 관련 기사

  1. 1

    How to turn off write protection in win 7 using diskpart?

  2. 2

    How to turn off Wireless power management permanently

  3. 3

    How to turn off screen (headless installation)

  4. 4

    Is it possible to turn off case insensitivity using pattern only?

  5. 5

    How do I turn off all the password prompts?

  6. 6

    How to turn off one of SATA HDDs installed in the computer to save power?

  7. 7

    How to turn off single user mode on a remote machine?

  8. 8

    How to turn off the boot up beep of Ubuntu Live USB?

  9. 9

    how to turn off password-less login for ssh?

  10. 10

    How to turn off one of SATA HDDs installed in the computer to save power?

  11. 11

    Turn off spellcheck in OneNote 2010

  12. 12

    Checkbox with Catel EventToCommand not working in Datagrid

  13. 13

    How to turn off auto start on-screen-keyboard on Windows 7 64 bit?

  14. 14

    How can I get the Quill editor authorship module to turn highlighting on and off?

  15. 15

    How can you turn Core Isolation Memory Integrity back off again in Windows 10 build 17127 and later

  16. 16

    How do I turn off bullet list formatting all of the lines to bullet lists in Word 2013?

  17. 17

    How do I turn off the annoying clicking sound when browsing web pages in Internet Explorer?

  18. 18

    How to get unchecked checkbox using Flask? (Or something!)

  19. 19

    Turn off the link hover statusbar in Google Chrome

  20. 20

    Redirect APPCRASH Dumps (Or turn them off)

  21. 21

    Windows 7 does not turn off monitor displays

  22. 22

    Extension to turn off certain page element?

  23. 23

    Turn off history for Durandal child router

  24. 24

    Windows 10: Laptop display refuses to turn off

  25. 25

    Android 5 turn off Interruptions filtration

  26. 26

    Chrome - Turn off bell for find in page no results?

  27. 27

    Turn off screen with password protection but no sleep

  28. 28

    Turn off auth in ubuntu 19.10 in all components

  29. 29

    Turn off smooth scrolling in GTK3

뜨겁다태그

보관