我有一个 WPF DataGrid,当我向它添加不适合其初始高度的数据时,它的高度会增加。除非用户增加窗口大小,否则我不希望改变高度。有没有办法阻止这种自动调整大小?
<Window x:Class="WpfDataGridSizeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Loaded="Window_Loaded"
SizeToContent="WidthAndHeight">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<DataGrid x:Name="wordsDataGrid" VerticalAlignment="Top" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" >
<DataGrid.Columns>
<DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Grid>
</Window>
public partial class MainWindow : Window
{
MyList myList = new MyList();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
wordsDataGrid.DataContext = myList;
for (int i = 0; i < 100; ++i)
{
myList.AddOne("blah blah");
}
}
}
public class MyList : ObservableCollection<AClass>
{
public MyList() { }
public void AddOne(string aString)
{
base.Add(new AClass(aString));
}
}
public class AClass
{
public string AString { get; set; }
public AClass(string aString)
{
AString = aString;
}
}