문제

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.

올바른 솔루션이 없습니다

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top