I have a DataGrid that is in one of a series of Expanders in a resizable window. When the DataGrid rows are loaded, the DataGrid extends off the bottom of the window without any scrollbars.
I've reduced the issue to the simplest elements I could below
I've tried putting the DataGrid in a separate ScrollViewer but had the same issue.
I also need the other two Expanders to remain visible in the window and not be pushed off the edge. I had a little success putting the three Expanders in a DockPanel, but the DataGrid still expanded to fill the entire window, pushing the other Expanders out of view.
XAML
<Window x:Class="WPFTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTestApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Name="root">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Expander Grid.Row="0" Name="Expander1" Expanded="Expander_Expanded">
<Expander.Header>Expander</Expander.Header>
<Expander.Content>
<ScrollViewer>
<DataGrid Grid.Row="1" Name="dataGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding A}" />
<DataGridTextColumn Header="B" Binding="{Binding B}" />
<DataGridTextColumn Header="C" Binding="{Binding C}" />
<DataGridTextColumn Header="D" Binding="{Binding D}" />
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Expander.Content>
</Expander>
<Expander Grid.Row="1" Name="Expander2" Expanded="Expander_Expanded">
<Expander.Content>
<TextBlock>...<LineBreak/>...<LineBreak/>...<LineBreak/>...<LineBreak/>...<LineBreak/>...</TextBlock>
</Expander.Content>
</Expander>
<Expander Grid.Row="2" Name="Expander3" Expanded="Expander_Expanded">
<Expander.Content>
<TextBlock>...<LineBreak/>...<LineBreak/>...<LineBreak/>...<LineBreak/>...<LineBreak/>...</TextBlock>
</Expander.Content>
</Expander>
</Grid>
</Window>
Codebehind
using System;
using System.Windows;
namespace WPFTestApp {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class Data {
public Guid A { get; set; }
public Guid B { get; set; }
public Guid C { get; set; }
public Guid D { get; set; }
public Data() {
A = Guid.NewGuid();
B = Guid.NewGuid();
C = Guid.NewGuid();
D = Guid.NewGuid();
}
}
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
for (int x = 0; x < 20; x++) {
dataGrid.Items.Add(new Data());
}
}
private void Expander_Expanded(object sender, RoutedEventArgs e) {
if (Expander1 != sender)
Expander1.IsExpanded = false;
if (Expander2 != sender)
Expander2.IsExpanded = false;
if (Expander3 != sender)
Expander3.IsExpanded = false;
}
}
}