Domanda

Is it possible to do the below (set the header double click event in data grid) in code behind? And if yes, how? Anything I searched did not show being possible to access the style of a data grid header!

<DataGrid ...> 
    <DataGrid.Resources>
        <Style TargetType="DataGridColumnHeader">
            <EventSetter Event="MouseDoubleClick" Handler="OnTableDoubleClick" />
        </Style>
    </DataGrid.Resources>
    ...

Many thanks.

Edit

After other attempts, I also tried this in the constructor of the CustomDataGrid class derived from DataGrid:

        Style _style = new Style(typeof(DataGridColumnHeader));
        _style.BasedOn = Application.Current.Resources["DataGridColumnHeaderStyle"] as Style;
        _style.Setters.Add(new EventSetter(MouseDoubleClickEvent, new MouseButtonEventHandler(OnTableDoubleClick)));
        this.ColumnHeaderStyle = _style;

If I well understood what I did this should add the double click event to the header style, but in fact it does nothing, meaning the handler is not called on runtime.

Nessuna soluzione corretta

Altri suggerimenti

You need to declare style on DataGridRowHeader:

    <DataGrid>
        <DataGrid.Resources>
            <Style TargetType="DataGridRowHeader">
                <EventSetter Event="MouseDoubleClick"
                             Handler="DataGridRow_MouseDoubleClick"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

In case you are talking about column header. Replace DataGridRowHeader with DataGridColumnHeader.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top