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;
}
if (column is DataGridTemplateColumn dgtc)instead of redundantly usingisandas. Why would you use theasoperator 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.Headeris a property of the DataGridColumn class, not just the DataGridTemplateColumn class. Simply writeforeach (var header in Columns.Select(c => (string)c.Header))in the OnEventHandler method.DataGridColumn, because I also deal withDataGridTexColumn, etc.CustomDataGridxxxGridwhich derives fromDataGridxxxColumnwith theExtendedHeaderproperty which allows me the distinguish different columns with the same text in the header, but different colours or groups.VirtualizingPanelto make your own 2D virtualized grid, but this is not a small task.