Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First underscore in a DataGridColumnHeader gets removed

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!

like image 644
Stuart Avatar asked Feb 22 '12 22:02

Stuart


2 Answers

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

like image 116
phoog Avatar answered Sep 18 '22 12:09

phoog


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("_", "__");
}
like image 43
umbreon222 Avatar answered Sep 18 '22 12:09

umbreon222