0

我有一个从 ObservableCollection 填充的 DataGrid。

                <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 项目是“正常的”(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>

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

有没有办法改变 StackPanel 案例,使其像“正常”案例一样,您可以单击标题中的任意位置对其进行排序?

4

1 回答 1

0

Replace the TextBox elements with TextBlock elements in the header, or set the IsHitTestVisible property of the TextBox elements to 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>
于 2020-04-01T05:56:46.337 回答