0

I built a rather complex UserControl based on a DataGrid.

I would like to show or hide specific columns from the view model via an EventAggregator. It works fine, but it is quite slow (I have more than 150 columns). Of course, I simplify the code below.

I suspect that the UI gets refreshed at each iteration. Is there a way to supend the UI refresh? I'm thinking about something like Application.ScreenUpdating = False in VBA.

public void OnEventHandler(AdjustColumnRequested e)
{
    var headers = e.Headers;

    foreach (var column in Columns)
    {
        var header = DataGridHelpers.GetHeader(column);

        column.Visibility = headers.Contains(header) ? Visibility.Visible : Visibility.Collapsed;
    }
}

Helper method to get the text in the header from the column which I don't believe is the issue.

public static string GetHeader(DataGridColumn column)
{
    if (column is DataGridTemplateColumn)
    {
        var dgtc = column as DataGridTemplateColumn;

        var header = (string)dgtc.Header;

        return header;
    }

    return null;
}
6
  • 1
    As a note, write if (column is DataGridTemplateColumn dgtc) instead of redundantly using is and as. Why would you use the as operator at all when you already known that column is a DataGridTemplateColumn? As seen in the next line, you do know how to use an explicit cast. Commented Jul 16 at 6:27
  • 1
    The whole type check and hence the whole method do not make sense, because Header is a property of the DataGridColumn class, not just the DataGridTemplateColumn class. Simply write foreach (var header in Columns.Select(c => (string)c.Header)) in the OnEventHandler method. Commented Jul 16 at 6:39
  • I don't think I can use pattern matching, because I use .NET Framework 4.6.0... I don't think it makes sense to modify how I get header from the DataGridColumn, because I also deal with DataGridTexColumn, etc. CustomDataGridxxxGrid which derives from DataGridxxxColumn with the ExtendedHeader property which allows me the distinguish different columns with the same text in the header, but different colours or groups. Commented Jul 17 at 12:35
  • Try changing the column(s) "width" to "0" instead of toggling visibility. Or use a "proxy" (UC) that replaces the original. And use "async void" event handlers. Commented Jul 19 at 0:08
  • None of these comments addresses the real issue. My opinion here is that you're fundamentally in trouble because DataGrid doesn't virtualize horizontally - only vertically. Simply put 150 columns (times however many rows are on the screen at once) is too many. DataGrid isn't meant for the equivalent of a spreadsheet. What you really need is virtualization both horizontally and vertically, and nothing out of the box in WPF gives you that. You could subclass VirtualizingPanel to make your own 2D virtualized grid, but this is not a small task. Commented Aug 12 at 18:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.