使用文本环绕调整 DataGrid 高度大小不正确
问题
我在 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 DataGrid
in 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加载后,在后面的代码中将该列的 TextWrapping 设置为 Wrap。为我工作。
Set the TextWrapping to Wrap for that column in your code behind after it's loaded. Worked for me.