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

时间:2022-10-26 22:18:50

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.

我有一个DataGridin是一个wpf应用程序。问题与某些窗口大小调整事件后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>

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; }
    }
}

1 个解决方案

#1


0  

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

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

#1


0  

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

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