WPF Layout Performance-Penalty using ColumnSpan - wpf

I have a Grid that looks something like the simplified example showed below. There are more elements in the grid but the problem is about the ListBox and the TextBlock.
If the TextBlock (or any other element with the same grid-configuration) has a ColumnSpan of two, every Update to an Item of the ListBox is very slow. I have looked with Perforator and VisualProfiler but could not see any special thing. If I set the second ColumnDefinition to a fixed width, all works as desired. The same happens, if I set the first RowHeight to a fixed height.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2">The column-span of this TextBlock seems to lead to an refresh-problem</TextBlock>
<ListBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding}"></ListBox>
</Grid>
Has someone a deeper insight into this and can explain me if this construction is a no no or what else here happens.

The problem is that the listbox creates all visual elements in the list to figure out which is the widest since your GridColumn is set to Auto in width. If your list contains many items or has a complicated (heavy) ItemTemplate, you're in trouble.
Normally, the ListBox only creates visual elements for visible elements (this is done by the VirtualizingStackPanel inside the ItemsPanelTemplate.
An alternative is to set a default width that will work 95% of the time - and then use a GridSplitter in the next column for the last 5% (So the user can widden the list). Alternate, you can use another Panel (e.g. a DockPanel) that doesn't ask the ListBox how wide it wants to be... :-)

Related

How do I bind the height of a StackPanel to the height of the containing Grid in WPF?

Im relatively new to WPF so I'm sorry in advance if there is a simple solution to this. However, I am trying to bind the height of a StackPanel to its containing Grid. I understand that a StackPanel automatically resizes to fit its elements but I would like to bind it to the Grid so that it does not do this. Right now the stack panel does not resize with the window since it remains the
size it needs to be in order to fit its elements.
Here is what I have so far:
<Grid x:Name="MainGrid" Grid.Row="2" Margin="1" Drop="Grid_Drop"
AllowDrop="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="175"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="SidePanel" Grid.Column="0" Height="{Binding
ActualHeight, ElementName=MainGrid, Mode=OneWay}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition/>
</Grid.RowDefinitions>
<local:DeviceTreeView x:Name="deviceTree" PyngInfo="{Binding
PyngSystemVm.PyngInfo}" Grid.Row="0"/>
</StackPanel>
<-- There is more code here but it is not important for answering this
question -->
</Grid>
I tried binding the height of "SidePanel" to the actual height of "MainGrid" but when I run this code and inspect the elements the Grid resizes with the window but the StackPanel does not. The StackPanel and the Grid even have different heights which doesn't make sense to me as their heights should be bound together.
I have also tried wrapping the entire StackPanel in a border and binding to that but that also did not work.
You don't need to bind the height of the StackPanel, simply setting its VerticalAlignment to Stretch will do it. However... You are still not going to get what I think you want. The StackPanel only stacks its child controls, it does not adjust them (at least not in the "stack" direction). Look into using a different control like Grid or UniformGrid or just expand your existing grid to have rows as well as columns.

Grid width "*" not behaving as expected when parent size is not defined

I have a set of controls in a dialog box that I want to auto-size. Everything worked fine until I tried to re-arange things using an evenly spaced Grid width.
Here's the XAML:
<Grid Margin="20" >
<Grid.RowDefinitions>
<RowDefinition Height="AUTO" />
<RowDefinition Height="AUTO" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid>
...left controls
</Grid>
<Border BorderThickness="1" BorderBrush="Gray" Grid.Column="1" Margin="0,10,0,5">
...right controls
</Border>
<Grid Grid.Row="2" Grid.ColumnSpan="2>
...okay and cancel
</Grid>
</Grid>
Here's the result:
Not what I'm expecting. The right column has a tiny width, for some reason it's not listening to the "*" and making it equal to the half the other column.
If I put a Width="450" on the highest Grid, or UserControl (or during runtime, resize the window it sits in!) Everything "jumps" and I get what I'm expecting, both columns evenly spaced:
But now it doesn't respond to re-sizing, or auto-size for larger content (except that wouldn't stretch when the parent container stretched) If I were after equal spacing I could use a shared size group. Is there anything I'm doing wrong or is this expected behavior for Width="*"?
When your outer Grid is hosted in a container where the width is not defined, the actual width will be the result of the descendant. That is, the (outer) Grid measure pass will "ask" to the right column (the Border): "I'm giving you whatever space you want: how much do you need?". As long the Border fragment won't require any specific size, the result is the default (zero, in most cases). Hence the collapsed behavior.
Basically, you should either define the outer Grid width, or the 2nd column width (by mean anything within the column should tell how much space it needs).
You are right * means to take size proportional to grid. It happens cause your the highest Grid does not enough space to be wider( when you set Width="450", then the Grid becomes wider).
If you have just one grid inside your Window, then the theSecondColumn will take all place at the right side(it will be wider as you want):
<Window x:Class="DataGridAddedColumns.MainWindow"
<!-- The code is omitted for the brevity -->
Title="MainWindow" Height="550" Width="525"
WindowStartupLocation="CenterScreen">
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Name="theSecondColumn" Width="*"/>
</Grid.ColumnDefinitions>
<Grid>
</Window>

How to: datagrid cell size changed when windows size changed

Firstly, my window is set to 900px height and 600px width. It contains a datagrid with 4 columns. I've created a template for cells, the widths are, say, 100px. Now when I change the window sizes, I want cells' width to grow proportionally to size of windows. I tried to add datagrid sizes change event but it didn't work.
How can I do this? Thanks!
Update: one more question: Have any way to make the sizes of a cell is the same whether they're empty or not.
In grid definition
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" MinWidth="70" />
<ColumnDefinition Width="Auto" />
...
MinWidth : Gets or sets the minimum width constraint of the element.

in wpf how do i make a datagrid fit the window height

I have a grid with 3 columns and 2 rows
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
I the lower left cell, i have a data grid, with AutoGenerateColumns=True which can load many rows. What I want to do is for the data grid height to maximize to fit the window, and for the user to be able to use the datagrid scrollbar to scroll the rows up and down.
What happens is that the datagrid flows of the bottom of the window, and even if i set the
ScrollViewer.VerticalScrollBarVisibility="Visible"
of the datagrid, the scrollbar has no effect and the rows flow downwards. Somehow the datagrid does not feel restricted...
What to do?
Try setting your DataGrid's HorizontalAlignment=Stretch and VerticalScrollBarVisibility=Auto
If that doesn't work, you may also need to bind the Grid's Height to the Window Height so that it doesn't auto-grow to fit its contents. Usually I use Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}" (It might be RenderSize.ActualHeight instead of just ActualHeight... I forgot.
Another alternative is to use a DockPanel instead of a Grid since that control doesn't auto-grow to fit its contents. Instead it'll stretch its last child to fill the remaining space.
I had the same problem but binding to the Window height did not completely solve the problem for me. In my case the DataGrid still extended 2 to 3 inches below the Window's viewable area. I believe this was because my DataGrid started about 2 to 3 inches below the top of the Window.
In the end I found that it was not necessary to bind the DataGrid's height at all. All I had to do was change the DataGrid's direct container.
For me, the following XAML setup causes the DataGrid to extend beyond the size of the Window when enough rows are added. Notice that the DataGrid sits within a StackPanel.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<!-- StackPanel Content accounting for about 2-3 inches of space -->
</StackPanel>
<!-- DataGrid within a StackPanel extends past the vertical space of the Window
and does not display vertical scroll bars. Even if I bind the height to Window
height the DataGrid content still extends 2-3 inches past the viewable Window area-->
<StackPanel Grid.Row="1">
<DataGrid ItemsSource="{StaticResource ImportedTransactionList}"
Margin="10,20,10,10" MinHeight="100">
</DataGrid>
</StackPanel>
</Grid>
However, simply removing the StackPanel fixed the problem for me.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<!-- StackPanel Content accounting for about 2-3 inches of space -->
</StackPanel>
<!-- Removing the StackPanel fixes the issue-->
<DataGrid Grid.Row="1" ItemsSource="{StaticResource SomeStaticResource}"
Margin="10,20,10,10" MinHeight="100">
</DataGrid>
</Grid>
As the original post is quite old, I should note that I am using VS2017 and .Net Framework 4.6.1 but I am not sure if this has any bearing.
You have to define the column and row where the DataGrid is with *.
You say it is on the lower left cell. The row is ok but your column there has Width="Auto".
The AutoGenerateColumns=True gives a mess if Width="Auto".

How to access actual Height of Elements with Height=Auto

I have a situation like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="560"/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <!-- status infos & content start -->
<RowDefinition Height="Auto" /> <!-- status infos -->
<RowDefinition Height="Auto" /> <!-- status infos -->
<RowDefinition Height="Auto" /> <!-- status infos -->
<RowDefinition Height="*"/> <!-- content ends -->
</Grid.RowDefinitions>
<!-- image a list of custom controls directed to the first or second column on all rows here -->
<SomeCustomControl Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2" />
</Grid>
As you can see I have 2 Columns, the right one is more or less reserved for status information, the left for content. "SomeCustomControl" contains a control so wide it needs to be set to ColumnSpan="2". Notice there are still the status control in the right column. In SomeCustomControl I have something like this:
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
[...]
<RowDefinition Height="Auto" />
<RowDefinition Height="*" /> <!-- problem control here -->
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- a list of Controls limited to the first column -->
<ProblemControl Grid.Column="0" Grid.ColumnSpan="2" />
</Grid>
Now, the first Rows of SomeCustomControl contain controls limited to the first column, then there is a row that contains my ProblemControl. The Height of the status controls is not predetermined and depends on the shown status information. The controls in SomeCustomControl that are limited to the first column also have different heights, that are currently determined automatically through the content.
I now have the problem that ProblemControl overlaps with some of my status controls. I tried to calculate the height of my status controls and the limited controls in SomeCustomControl, but as all controls are sized dynamically I can't seem to get correct Heights. The Height of the RowDefinitions all contains Heights of type Auto and value 1, the Heights of the concrete Controls seems to be NaN.
Any ideas as to how I can calc the heights or prevent the overlapings in other ways.
I've encountered somewhat of the same problem, but came across the solution recently. The reason why you can't access the width and height properties of a control with width or height set to Auto is that the run time system is querying for the property values before they've been set. The properties ActualWidth and ActualHeight claim to get the rendered height of controls so in theory, you'd think you could simply wait until the SL application had finished loading and then perform your query, since the controls would be rendered by then and therefore, the ActualHeight/ActualWidth values should be set.
Sadly, this isn't the case either. There doesn't seem to be any guarantee when those values are set, so the workaround I used is to hook into the SizeChanged-event of the control whose values I want. SizeChanged is triggered whenever the width and height properties of a control are changed, so if I handle that event, I am guaranteed that the values are set to something other than NaN.
Do whatever logic you need to perform in a handler for that event, and you'll find the values are set.
Have you tried with ActualHeight property on control ?
You ought to be able to solve this with proper use of the Grid and some panels (DockPanel and StackPanel can be quite useful). You may also need the HorizontalAlignment or VerticalAlignment properties set to Stretch on some controls.
Your example doesn't have enough in it for us to duplicate your problem or to know that we've addressed it correctly. If you'd like more specific help, please expand the example so we can run it and see your issue.

Resources