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

时间:2012-01-14 18:26:58

标签: c# wpf xaml datagrid wpfdatagrid

问题

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

1 个答案:

答案 0 :(得分:0)

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