Search code examples
c#xmlwpfdatagrid

Strange behaviour when collapsing lines in XML bound WPF Datagrid


i am using a wpf datagrid with a xml file as DataContext. All working good except for iterating thorough the table and collapsing individual rows. There are several checkboxes where the user can decide which kinds of rows he wants to see, dependent on their error level string.

If a checkbox is checked, some of the rows are collapsed, others not. You need to uncheck the checkbox and check it again to collapse the ones of the first try and some of the others. If you recheck it again more rows are collapsed every time. I guess it has something to do with how much of the list is actually visible and how much not because of the window size.

Thanks in advance.

foreach (DataGridRow r in rows)
        {
            bool showRow = true;

                var tb = Datagrid.GetCell(dataGridEvents, r, 2).Content;
                string level = ((TextBlock)tb).Text;

                switch (level)
                {
                    case "Warning":
                        showRow = checkBoxWarnings.IsChecked.HasValue ? checkBoxWarnings.IsChecked.Value : false;
                        break;
                    case "Critical":
                        showRow = checkBoxCritical.IsChecked.HasValue ? checkBoxCritical.IsChecked.Value : false;
                        break;
                    case "OK":
                        showRow = checkBoxOK.IsChecked.HasValue ? checkBoxOK.IsChecked.Value : false;
                        break;
                    case "Unknown":
                        showRow = checkBoxUnknown.IsChecked.HasValue ? checkBoxUnknown.IsChecked.Value : false;
                        break;
                }                
            r.Visibility = showRow ? Visibility.Visible : Visibility.Collapsed;
        }

Solution

  • I guess it has something to do with how much of the list is actually visible and how much not because of the window size.
    I think that you are correct.

    In my opinion you should avoid using visibility any way and you're better off using ICollectionView for DataContext and filter it on the CheckBox events.
    You can find an example here How to: Group, Sort, and Filter Data in the DataGrid Control