How do I get my datagrid to grow with a window and save fields?

Everything regarding my layout will be current with the resizing of the main window. The problem I am facing is that, as you can see, the datagrid is disconnecting from the screen. If you maximize the window, the datagrid will resize using the window, but continue to exit the screen. How do I get it to keep its margin at 20 using the parent grid?

<Grid> <StackPanel Orientation="Vertical"> <TextBox Height="170" Name="txtSQL" VerticalAlignment="Top" AcceptsReturn="True" TextWrapping="Wrap" Margin="20"/> <Button Content="Run!" Height="23" HorizontalAlignment="Left" Name="btnRun" VerticalAlignment="Top" Margin="20,0,0,0" Width="75" Click="btnRun_Click" /> <Grid> <my:DataGrid Name="dgResults" VerticalAlignment="Top" Margin="20" /> </Grid> </StackPanel> </Grid> 

enter image description here

UPDATE: Just to be more specific. The effect I'm trying to accomplish is this:

When the window first loads, you are given an empty datagrid, so it only has about 15 pixels. When you run the query, it will populate the datagrid, reassigning the itemssource. At the moment, when you do this, if the data exceeds the size of the window, it will disappear at the bottom of the screen. I need it to only expand to the bottom of the window and then activate the scroll bar. I can do this by simply wrapping it in a scrollviewer. However, when the window size changes, the datagrid needs to be resized.

I am wondering if this setup can have anything to do with this. The form is a wpf page displayed in the frame.

UPDATE:

 <Page x:Class="Hold_Database___Prototype_1.Views.SQL" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="304" d:DesignWidth="732" Title="SQL" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" AllowDrop="True"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="23" /> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBox Height="170" Name="txtSQL" VerticalAlignment="Top" AcceptsReturn="True" TextWrapping="Wrap" Margin="20" Grid.Row="0"/> <Button Content="Run!" Height="23" HorizontalAlignment="Left" Name="btnRun" VerticalAlignment="Top" Margin="20,0,0,0" Width="75" Grid.Row="1" Click="btnRun_Click" /> <DockPanel Grid.Row="2"> <my:DataGrid Name="dgResults" Margin="20" /> </DockPanel> </Grid> </Page> 
+4
source share
3 answers

What is the dock panel in this example?

Try putting a DataGrid directly in a cell without a stack panel. If you set the height of the button, set the grid to auto.

In addition, why devote so much space to the text?

  <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBox Height="170" Name="txtSQL" VerticalAlignment="Top" AcceptsReturn="True" TextWrapping="Wrap" Margin="20" Grid.Row="0"/> <Button Content="Run!" Height="23" HorizontalAlignment="Left" Name="btnRun" VerticalAlignment="Top" Margin="20,0,0,0" Width="75" Grid.Row="1" Click="btnRun_Click" /> <my:DataGrid Grid.Row="2" <my:DataGrid Name="dgResults" Margin="20" /> 

Then also set HorizontalAlignment , VerticleAlignment , HorizontalContentAlignment and VerticalContentAlignment = stretch

+2
source

Your Grid is displayed without a scroll bar because you are using a StackPanel . Caution should be exercised when using the StackPanel , which is the simplest of all WPF Panel derived classes, because its MeasureOverride calls Measure for all its children with a double.PositiveInifity size, regardless of how much space the panel is actually available. Even ScrollViewer will not help you with StackPanel ( ScrollBar will be shown, but you will not be able to move it).

For example, consider Window 350 in height and width and the Button unit as its contents, which is 500 in height and width:

 <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="350"> <StackPanel> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Button Content="Hello" Height="500" Width="500" /> </ScrollViewer> </StackPanel> </Window> 

As in your example, the Button here is drawn in both vertical and horizontal directions, and there will be a non-functional scrollbar. If we change the panel to one that matches the size of its specified area (for example, DockPanel ):

 <Window x:Class="WpfApplication2.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"> <DockPanel> <ScrollViewer> <Button Content="Hello" Height="500" /> </ScrollViewer> </DockPanel> </Window> 

then scrollbars will appear that are functional and therefore allow content to be displayed off-screen by scrolling.

Hope this helps!

+2
source

What I did was define the horizontal and vertical alignment to β€œstretch” respectively in the control that you want (your DataGrid) to resize based on the resizing of the window.

0
source

Source: https://habr.com/ru/post/1387815/


All Articles