修复 DataGrid Header 使用 StackPanel 在 user-defined header 上排序

Fixing DataGrid Header Sort on user-defined header using a StackPanel

我有一个 DataGrid,由 ObservableCollection 填充。

                <Style x:Key="DGHdr_2LineNormal" TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Height"                       Value="40" />
                <Setter Property="Margin"                       Value="0,2,0,0" />
                <Setter Property="VerticalAlignment"            Value="Bottom" />
                <Setter Property="VerticalContentAlignment"     Value="Bottom" />
                <Setter Property="HorizontalAlignment"          Value="Stretch" />
                <Setter Property="HorizontalContentAlignment"   Value="Center" />
            </Style>

            ...........................

        <DataGrid Grid.Row="1"
                  AutoGenerateColumns="False"
                  IsReadOnly="True"
                  SelectionMode="Single"
                  SelectionUnit="FullRow"
                  GridLinesVisibility="None"
                  Name="MatipBatapConfigList"               
                  ColumnHeaderStyle="{StaticResource DGHdr_2LineNormal}"
                  ItemsSource="{Binding}">
            ...........................
        </DataGrid>

几个 ColumnHeaderStyle 项目是 "normal"(1 行文本):

                <DataGridTextColumn Binding="{Binding Path=CurrentItem.hdrSystemName}"
                        Header="Name">

但对于某些人,我使用 StackPanel,这样我就可以有多行,使用 TextBox 项目, 改变它们之间的字体大小:

            <Style x:Key="DGHdr_2LineStackPanel" TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Width"                        Value="Auto" />
                <Setter Property="Height"                       Value="40" />
                <Setter Property="Margin"                       Value="0,0,0,-2" />
                <Setter Property="HorizontalAlignment"          Value="Stretch" />
                <Setter Property="HorizontalContentAlignment"   Value="Center" />
            </Style>

            ...........................

                <DataGridTextColumn Binding="{Binding Path=CurrentItem.DestAddr}"
                        HeaderStyle="{StaticResource DGHdr_2LineStackPanel}" >
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Vertical" Margin="0,0,0,-2">
                            <TextBox Text="Destination" IsReadOnly="True"
                                     HorizontalAlignment="Center" Margin="0" BorderThickness="0" FontSize="9"
                                     VerticalAlignment="Bottom" Width="Auto" Padding="0" Height="16" Background="Transparent"/>
                            <TextBox Text="Address"  IsReadOnly="True"
                                     HorizontalAlignment="Center" Margin="0" BorderThickness="0"
                                     VerticalAlignment="Bottom" Width="Auto" Padding="0" Height="18" Background="Transparent"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

我遇到的问题是对每一列进行排序。在 "normal" 的情况下,我可以单击 header 列的任意位置对其进行排序。但是,在 StackPanel 的情况下,顶部(排序箭头的高度)只有一个 4 像素高的薄区域,您可以在其中对列进行排序。

有没有办法更改 StackPanel 案例,使其像 "normal" 案例一样工作,您可以单击 header 中的任意位置对其进行排序?

TextBox元素替换为header中的TextBlock元素,或者将TextBox元素的IsHitTestVisible属性设置为false:

<StackPanel Orientation="Vertical" Margin="0,0,0,-2">
    <TextBox Text="Destination" IsReadOnly="True" IsHitTestVisible="False"
                                HorizontalAlignment="Center" Margin="0" BorderThickness="0" FontSize="9"
                                VerticalAlignment="Bottom" Width="Auto" Padding="0" Height="16" Background="Transparent"/>
    <TextBox Text="Address"  IsReadOnly="True" IsHitTestVisible="False"
                            HorizontalAlignment="Center" Margin="0" BorderThickness="0"
                            VerticalAlignment="Bottom" Width="Auto" Padding="0" Height="18" Background="Transparent"/>
</StackPanel>