简体   繁体   中英

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!

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("_", "__");
}

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("_", "__");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM