C# WPF DataGrid调整内部列标题的大小会导致极端的滚动

C# WPF DataGrid调整内部列标题的大小会导致极端的滚动,c#,wpf,datagrid,wpf-controls,C#,Wpf,Datagrid,Wpf Controls,我发现了一个关于股票WPF数据网格的行为,我无法解释或阻止,但它感觉像一个bug。我很好奇是否有人遇到过这种情况,以及解决方法是什么 复制步骤: 当使用具有多个列的DataGrid(在我的测试应用程序中为4列)时,请调整标题大小,以便显示水平滚动条 一直向右滚动 开始调整不是第一列或最后一列的任何列的大小 将列向左调整到最小值 然后将其向右拖动,您将看到该列的大小已调整为最大宽度 要使列恢复到适当的大小有些困难。下面是一个简单的WPF应用程序,用于演示该问题 MainWindow.xaml &l

我发现了一个关于股票WPF数据网格的行为,我无法解释或阻止,但它感觉像一个bug。我很好奇是否有人遇到过这种情况,以及解决方法是什么

复制步骤:

  • 当使用具有多个列的DataGrid(在我的测试应用程序中为4列)时,请调整标题大小,以便显示水平滚动条
  • 一直向右滚动
  • 开始调整不是第一列或最后一列的任何列的大小
  • 将列向左调整到最小值
  • 然后将其向右拖动,您将看到该列的大小已调整为最大宽度
  • 要使列恢复到适当的大小有些困难。下面是一个简单的WPF应用程序,用于演示该问题

    MainWindow.xaml

    <Window x:Class="TestGridColumnSpacing.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TestGridColumnSpacing"
            mc:Ignorable="d"
            Title="MainWindow"
            Height="450"
            Width="750">
    
        <Grid>
            <DataGrid x:Name="TheDataGrid" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Field 1" Binding="{Binding Field_1}" />
                    <DataGridTextColumn Header="Field 2" Binding="{Binding Field_2}" />
                    <DataGridTextColumn Header="Field 3" Binding="{Binding Field_3}" />
                    <DataGridTextColumn Header="Field 4" Binding="{Binding Field_4}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    
    我一直在寻找一个解决方案或其他开发人员谈论它,但我找不到任何提及,这是令人惊讶的,因为它是多么容易复制。请分享您关于解决方案的所有信息

    namespace TestGridColumnSpacing
    {
        using System.Windows;
    
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                this.Loaded += MainWindowLoaded;
            }
    
            private void MainWindowLoaded(object sender, RoutedEventArgs e)
            {
                this.TheDataGrid.Items.Add(new { Field_1 = "", Field_2 = "", Field_3 = "", Field_4 = "Step 1: Using right thumb on Field 4 header, resize enough to make horizontal scrollbar visible if not visible already." });
                this.TheDataGrid.Items.Add(new { Field_1 = "", Field_2 = "", Field_3 = "", Field_4 = "Step 2: Scroll horizontal scrollbar entirely to the right." });
                this.TheDataGrid.Items.Add(new { Field_1 = "", Field_2 = "", Field_3 = "", Field_4 = "Step 3: Using right thumb of Field 3, resize to the right.\nThis will increase the width of Field 3. Notice normal manner of the scrollbar's size changing." });
                this.TheDataGrid.Items.Add(new { Field_1 = "", Field_2 = "", Field_3 = "", Field_4 = "Step 4: Using right thumb of Field 3, resize the column leftward until Field 3 approaches its minimum width.\nYou'll notice when all of Field 4 is in view, Field 3 will continue to collapse and appear to shrink towards Field 4." });
                this.TheDataGrid.Items.Add(new { Field_1 = "", Field_2 = "", Field_3 = "", Field_4 = "Step 5: At this point, continue using the right thumb of Field 3 to resize the column.\nNotice exaggerated resizing of the scrollbar."});
            }
        }
    }