DataTemplate of Grid inside ListView is not resizing - wpf

I have a ListView that lays on top of UI graphics in my UserControl that I want to show and size using a Grid that matches the parent grid. I'm trying to template the way each item shown and sized using the ListView.ItemTemplate. The issue I'm having is that the DataTemplate is not sizing to the main Grid of the UI, and I'm not sure why.
This is what I've done, I think it a fairly simple and straight forward datatemplate:
<ListView ItemsSource="{Binding Path=StatusFilter}" Grid.Row="2" Grid.ColumnSpan="12">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="92"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding Path=ECNNumber}" Grid.Column="0"/>
<Label Content="{Binding Path=DateECNDue}" Grid.Column="1"/>
<Label Content="{Binding Path=Model.Model}" Grid.Column="2"/>
<--More stuff for each column-->
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When this is displayed the grid is completely bypassed and each element is shown like a horizontally aligned stackpanel. To test that my DataTemplate wasn't faulty I threw it into an ItemsControl and it worked perfectly. It resized with the parent grid without any issues.
Why isn't this working for the ListView, and how do I get it to work? I need the selection and scrollview capability of the ListView otherwise I'd be using the ItemsControl.
I read something about Grid.SharedSizeScope but that doesn't work with dynamic sizing of grid, and that's all I could really find on this issue.

Related

How to make a TextBlock resize horizontally in a LsitBox

I've got a window with a ListBox containing TextBlocks in the template:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
...
<ListBox x:Name="AnnotationsList" ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.Row="1" Grid.ColumnSpan="3">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding DateAdded}" Grid.Column="0"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I expected the TextBlock to adjust the size, with both elements in the template filling the width together in 1/20 proportions.
Instead, the TextBlock never gets smaller and ListBox gets a horizonal scrollbar.
How can I keep the list scrollable vertically and make sure the text gets narrower and wraps if I resize the window?
As Clemens answered in the comments, HorizontalScrollBarVisibility has an option of Disabled, which doesn't affect just the visibility, but actually triggers the children elements to get limited by the horizontal size of the container.

Width of Textbox, which placed in grid column, extends when inserting long text

When I put a Textbox in a grid column like below
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="115"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="90"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="hello"/>
<TextBox Grid.Column="1" />
<Button Grid.Column="2" Content="push me"/>
</Grid>
</StackPanel>
I get proper result, i.e. textbox width is get from parent grid
But when I type a long text, the textbox starts exceeding its column and it stops extending after several extra letters
To .Net 4.6.2, I get same result but changing to .Net 4.7.2 the problem is solved i.e. the textbox width is not changing.
My software is compiled .Net 4.0, is there a solution to solve this for lower .net than 4.7.2?
Tried Pavel's first idea: removing stackpanel and adding another grid row in, still not working in .net 4.6.2
Tried Pavel's second idea: making the width of first column "Auto" instead of "1*". This works, the textbox doesn't extend (.net 4.6.2), however I really wanted the first and second column be responsive to resize.
You can solve this be removing the StackPanel and adding RowDefinition to Grid. You can also set TextWrapping="Wrap" for TextBox for wrapping a long text
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="90"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="hello" MinWidth="115"/>
<TextBox Grid.Column="1" TextWrapping="Wrap"/>
<Button Grid.Column="2" Content="push me"/>
</Grid>
StackPanel Y dimension is constrained to content, it will be automatically expanded to its content, more details can be found at panels overview
To me this is a bug that Textbox text size can change its parent width (grid width). This is happening in VS2019 .Net 4->4.6.2 but not in and after .Net 4.7.2.
For anybody faces this problem, I found below workaround by defining an invisible grid which contains textblocks. I get the widths of columns and give them to controls placed in a StackPanel.
<some container>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" MinWidth="115"/>
<ColumnDefinition Width="3*" />
<ColumnDefinition MinWidth="90"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<TextBlock x:Name="C0" Grid.Column="0" />
<TextBlock x:Name="C1" Grid.Column="1" Margin="5"/>
<TextBlock x:Name="C2" Grid.Column="2" />
</Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Width="{Binding ElementName=C0, Path=ActualWidth }" Text="hello" />
<TextBox Width="{Binding ElementName=C1, Path=ActualWidth }" Margin="5" />
<Button Width="{Binding ElementName=C2, Path=ActualWidth }" Content="push me"/>
</StackPanel>
</some container>
The margin should be the same for column in grid and control in StackPanel (see second column).

Auto Sizing between grid columns

I have two controls and a GridSplitter .
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<UserControlOne Grid.Colum="0" Visibility="{Binding MyProperty1}"/>
<GridSplitter Visibility="{Binding MyProperty1}" m:Splitterbehaviour.Apply= true/>
<UserControlTwo Grid.Colum="1" />
</Grid>
I am trying to show/hide the UserControlOne with the MyProperty1 which is working fine but when it is hidden i want the UsercontrolTwo to take whole page space. I could easily achieve this by using a stack or dock panel. But if i use the stackpanel or dockpanel my GridSplitter wont work.(I have a behaviour set to GridSplitter which will identify the first column and it will help to resize the first and second column)
I don't see how that splitter is working
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<UserControlOne Grid.Colum="0" Visibility="{Binding MyProperty1}"/>
<UserControlTwo Grid.Colum="1"/>
</Grid>

Autosizing a grid column to take up remaining space in parent

In WPF, I am having a heck of a time trying to get a grid to size properly.
I have the following layout for my grid:
<ItemsControl HorizontalContentAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" />
<Label Grid.Column="1"/>
<TextBox Grid.Column="2"/>
<Button Grid.Column="3"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The problem is that Width="Auto" seems to be sizing that column to the width of the content, and not filling out the extra space in the parent container. This leaves the rest of the columns all unaligned, and ugly blank space at the end of each row.
I'm probably missing something simple, but I can't seem to find a method to fit the column appropriately.
Or is there a better control for the job?
* means fill or share. If you had two with * then they would share the width evenly.
<ColumnDefinition Width="*"/>
Seems I found a solution after a bit more tinkering around.
The problem was: <ColumnDefinition Width="Auto"/>
This was causing the column to fit to the content. I changed it to: <ColumnDefinition />
This causes the column to fit to the empty space left in the parent container, regardless of content size.

WPF - Resizing Children to Fill a Parent

In WPF, I simply want to have a 'container' which has 3 textblocks. I would like these 3 textblocks to be sized so they each take up 1/3 of the parent's width. I noticed that the stackpanel sizes the last child automatically, but is there a way to size each child automatically?
Use a Grid or a UniformGrid
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="1" Grid.Column="0"/>
<TextBlock Text="2" Grid.Column="1"/>
<TextBlock Text="3" Grid.Column="2"/>
</Grid>
The grid is very common and in every WPF-App used a lot. However also the UniformGrid is very handy but not so well known.

Resources