【问题标题】:Improper DataGrid Height Resizing with Text Wrapping使用文本换行调整不正确的 DataGrid 高度
【发布时间】:2012-01-14 18:26:58
【问题描述】:

问题

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

【问题讨论】:

  • 我遇到了同样的问题,单元格高度没有回落。我最终选择了 ListView GridView 和大量手动代码。抱歉,我无法发布代码,因为它在技术上属于客户。边框太长是什么意思?
  • 我的单元格高度工作正常,我使用Width="Auto"Width="*" 来解决这个问题。我的问题是DataGrid 本身没有正确调整大小。发生这种情况时,看起来DataGrid 周围有一个Border,但大小不合适(太长)。
  • 如果您删除 StackPanel,问题是否仍然存在?默认情况下,StackPanel 将为其子级提供无限的高度,这通常会导致大小问题。如果方向设置为水平,那么孩子的宽度将是无限的。
  • StackPanel 不是导致此问题的原因(我尝试将其删除以防万一)。 StackPanel 为每个 UIElement 提供了所需的垂直空间,但仍像 VerticalAlignment=Top 一样显示它们。实际上,通过去掉 StackPanel,DataGrid 总是会占用窗口的大小(甚至比以前更糟)。
  • 是的,我有同样的 DataGrid 大小作为一个整体问题。就像我进入 Detail 模板然后关闭它一样,DataGrid 不会获得空间。我没有解决办法。

标签: c# wpf xaml datagrid wpfdatagrid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多