First underscore in a DataGridColumnHeader gets removed
I like umbreon222's solution. If you're like me and use DataGrids all the time and have a library you always reference (like I do), you can create a child of the DataGrid class that registers that event handler all the time:
using System.Windows.Controls;
namespace MCLBZ7.Controls
{
public class MCLDataGrid : DataGrid
{
public MCLDataGrid() : base()
{
this.AutoGeneratingColumn += DG_AG_Header;
}
private void DG_AG_Header(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string header = e.Column.Header.ToString();
e.Column.Header = header.Replace("_", "__");
}
}
}
It's because of AccessKey handling. Just write an event handler like this to temporarily escape the underscores in the datagrid header.
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string header = e.Column.Header.ToString();
// Replace all underscores with two underscores, to prevent AccessKey handling
e.Column.Header = header.Replace("_", "__");
}
This blog post says that you can escape the underscore by doubling it: "data__grid_thing"
.
Another approach can be found in the accepted answer to this question