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

问题描述

我发现WPF DataGrid的一种行为无法解释或阻止,但感觉像是个错误。我很好奇是否有人遇到了这个问题,以及解决方法解决方法是什么。

复制步骤:

  1. 在使用具有几列的DataGrid(在我的测试应用中为4)时,请调整标题的大小,以便显示水平滚动条。
  2. 一直滚动到右侧。
  3. 开始调整不是第一列或最后一列的任何列的大小。
  4. 将列的大小向左调整为最小。
  5. 然后将其拖动到右侧,您会看到该列的大小已调整为一个极端的宽度。

将列恢复到适当的大小有些困难。下面是一个简单的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>

MainWindow.xaml.cs

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_4 = "Step 2: Scroll horizontal scrollbar entirely to the right." });
            this.TheDataGrid.Items.Add(new { Field_1 = "",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_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_4 = "Step 5: At this point,continue using the right thumb of Field 3 to resize the column.\nNotice exaggerated resizing of the scrollbar."});
        }
    }
}

我一直在寻找解决方案或其他开发人员在谈论它,但是我找不到任何提及,这是令人惊讶的,因为它是如此容易复制。请分享解决方案的所有信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...