使用文本环绕调整 DataGrid 高度大小不正确

发布于 2024-12-26 14:57:25 字数 3173 浏览 0 评论 0原文

问题

我在 wpf 应用程序中有一个 DataGrid。 该问题与 DataGrid 在某些窗口大小调整事件后的显示方式有关。 每当用户缩小窗口然后再次放大时,DataGrid 的行会再次缩小(由于文本换行),但 DataGrid 本身的高度不会变小不缩水。 结果是 DataGrid 周围的边框似乎太长。 一旦用户降低窗口的高度,该边框就会缩小。当应用程序启动时,该边框也太长。

Xaml

<Window x:Class="SampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="WrappingTextBlock" TargetType="TextBlock">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
        <Style x:Key="WrappingTextBox" TargetType="TextBox">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
    </Window.Resources>

    <Grid>
        <Border>
            <ScrollViewer>
                <StackPanel>
                    <DataGrid ItemsSource="{Binding Objects}" HorizontalAlignment="Stretch" Margin="5" AutoGenerateColumns="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled">
                        <DataGrid.Columns>
                            <DataGridTextColumn MinWidth="15" Width="Auto" Header="#" Binding="{Binding Number}"/>
                            <DataGridTextColumn MinWidth="65" Width="Auto" Header="Style" Binding="{Binding Style}"/>
                            <DataGridTextColumn MinWidth="80" Width="*" Header="Description" Binding="{Binding Description}" ElementStyle="{StaticResource WrappingTextBlock}" EditingElementStyle="{StaticResource WrappingTextBox}"/>
                        </DataGrid.Columns>
                    </DataGrid>                                      
                </StackPanel>
            </ScrollViewer>
        </Border>
    </Grid>
</Window>

代码隐藏

using System.Collections.Generic;
using System.Windows;

namespace SampleApp {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }

    public class ViewModel {
        public ViewModel() {
            Objects = new List<MyObject>() {
                new MyObject() { Number=1, Style="Good Style", Description="Small description", },
                new MyObject() { Number=2, Style="Bad Style", Description="This is a medium length description that you are reading.", },
                new MyObject() { Number=3, Style="Awesome Style", Description="This is a long description that you are reading because I repeat the message. This is a long description that you are reading because I repeat the message.", },
            };
        }
        public List<MyObject> Objects { get; set; }
    }

    public class MyObject {
        public MyObject() { }
        public int Number { get; set; }
        public string Style { get; set; }
        public string Description { get; set; }
    }
}

The Problem

I have a DataGridin a wpf application.
The problem has to do with how the DataGrid looks after certain window resizing events.
Whenever the user shrinks the window and then enlarges it again, the rows of the DataGrid shrink back down (because of the text wrapping), but the height of the DataGrid itself doesn't shrink.
The resulting effect is that there appears to be a border around the DataGrid that is too long.
This border shrinks back down as soon as the user decreases the height of the window. This border is also too long when the application is started.

Xaml

<Window x:Class="SampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="WrappingTextBlock" TargetType="TextBlock">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
        <Style x:Key="WrappingTextBox" TargetType="TextBox">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
    </Window.Resources>

    <Grid>
        <Border>
            <ScrollViewer>
                <StackPanel>
                    <DataGrid ItemsSource="{Binding Objects}" HorizontalAlignment="Stretch" Margin="5" AutoGenerateColumns="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled">
                        <DataGrid.Columns>
                            <DataGridTextColumn MinWidth="15" Width="Auto" Header="#" Binding="{Binding Number}"/>
                            <DataGridTextColumn MinWidth="65" Width="Auto" Header="Style" Binding="{Binding Style}"/>
                            <DataGridTextColumn MinWidth="80" Width="*" Header="Description" Binding="{Binding Description}" ElementStyle="{StaticResource WrappingTextBlock}" EditingElementStyle="{StaticResource WrappingTextBox}"/>
                        </DataGrid.Columns>
                    </DataGrid>                                      
                </StackPanel>
            </ScrollViewer>
        </Border>
    </Grid>
</Window>

Code-Behind

using System.Collections.Generic;
using System.Windows;

namespace SampleApp {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }

    public class ViewModel {
        public ViewModel() {
            Objects = new List<MyObject>() {
                new MyObject() { Number=1, Style="Good Style", Description="Small description", },
                new MyObject() { Number=2, Style="Bad Style", Description="This is a medium length description that you are reading.", },
                new MyObject() { Number=3, Style="Awesome Style", Description="This is a long description that you are reading because I repeat the message. This is a long description that you are reading because I repeat the message.", },
            };
        }
        public List<MyObject> Objects { get; set; }
    }

    public class MyObject {
        public MyObject() { }
        public int Number { get; set; }
        public string Style { get; set; }
        public string Description { get; set; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画离情绘悲伤 2025-01-02 14:57:25

加载后,在后面的代码中将该列的 TextWrapping 设置为 Wrap。为我工作。

Set the TextWrapping to Wrap for that column in your code behind after it's loaded. Worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文