I'm having a problem where I have a DataGridColumnHeader that is receiving text with underscores as the content, and the first underscore is hidden unless you press alt ("data_grid_thing" displays as 'datagrid_thing"). I searched around for a bit, and found some solutions to this problem for Labels, since if you turn RecognizesAccessKey to false, then the text won't be considered 'AccessText' (. This however doesn't work for DataGridColumnHeader, as it removes all the other styling, and so instead of a header with text inside of it, I just get whitespace with text. I tried using the BasedOn property as well to no effect.
I am open to solutions either through the C# side (modifying the RecognizesAccessKey property by somehow finding the ContentPresenter perhaps), or through modification of XAML (figuring out a way to preserve the default style).
My XAML looks something like this:
<Style x:Key="DataGridColumnHeaderStyle" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridColumnHeader">
<Border>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Thanks!
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
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("_", "__");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With