TextBlocks Not Filling Available Space
I have created a grid in WPF, with the RowDefinitions being defined programmatically based on a variable. I would like for each row to take up an equal space such that the window is filled, however this doesn't seem to be happening. Any help would be greatly appreciated!



18 Replies
@jim.jam.1 did you try debugging it?
i guess that datacontext isnt set yet when the ctor of mainwindow is called
DataContext=“{property}”
In the picture you have no bindings . Just create a binding.
Sorry, didn't include that part in the original screenshot for some reason:

Your items control will make rows automatically
Could use a stack panel and add a gap in your template
Spacing I mean
basically the goal is I want x number of rows (determined by a config) The rows should all be even height, taking up the full height of the window. Even if there are only two items, these should occupy just the first two rows. Is that achievable?
I would start with one items control and make a stack panel orientation horizontal and then add your capcode and message as children . No need for two controls . Especially when coming from the bindings of message
Horizontal Alignment and vertical centered too .
On your grid
To fill any space alignment is stretched
Additional I would control the follow of your information using linq on any list just do a .take(n)
Bindings will update to the number of data items you provide.
For some reason it's still not consuming vertical space


put Itemscontrol add proptery for HorizontialAlignment= Strech
you can add to the StackPanel Spacing="n" too
I've added Horiontal and Vertical alignments = "Stretch" to the ItemsControl however it hasn't actually done anything. UI design always loses me...
For reference:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<ItemsControl
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{x:Bind Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Spacing="24">
<TextBlock Text="{Binding Name}" Width="100"/>
<TextBlock Text="{Binding Message}" Width="200" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>

how does this look. ?
public class MessageEntry
{
public string Name { get; set; }
public string Message { get; set; }
}
public ObservableCollection<MessageEntry> Items { get; set; } = new();
public MainWindow() { this.InitializeComponent(); // add some items to the list using lori upsium Items.Add(new MessageEntry { Name = "Alice", Message = "Hi, nice to meet you!" }); Items.Add(new MessageEntry { Name = "Bob", Message = "Good morning, everyone!" }); Items.Add(new MessageEntry { Name = "Charlie", Message = "How's it going?" }); Items.Add(new MessageEntry { Name = "Diana", Message = "Looking forward to the weekend!" }); Items.Add(new MessageEntry { Name = "Eve", Message = "Let's catch up soon!" }); } You TextBlocks are only going to occupy space enough for the text. That's why I added width to each. Another way list View
public MainWindow() { this.InitializeComponent(); // add some items to the list using lori upsium Items.Add(new MessageEntry { Name = "Alice", Message = "Hi, nice to meet you!" }); Items.Add(new MessageEntry { Name = "Bob", Message = "Good morning, everyone!" }); Items.Add(new MessageEntry { Name = "Charlie", Message = "How's it going?" }); Items.Add(new MessageEntry { Name = "Diana", Message = "Looking forward to the weekend!" }); Items.Add(new MessageEntry { Name = "Eve", Message = "Let's catch up soon!" }); } You TextBlocks are only going to occupy space enough for the text. That's why I added width to each. Another way list View

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView ItemsSource="{x:Bind Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}" />
<TextBlock Grid.Column="1" Text="{Binding Message}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
That's a bit more what I'm going for, except still needing the heights of each row to be even and take up the full height of the window. Is that still possible?
Then you do a MaxHeight on the Text Blocks
MinHeight i believe
Hold on I do something...
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView ItemsSource="{x:Bind Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
MinHeight="100"
Text="{Binding Name}" />
<TextBlock
Grid.Column="1"
MinHeight="100"
MaxLines="3"
Text="{Binding Message}"
TextWrapping="WrapWholeWords" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>

this ensures that all your textblocks are going to be the same size, I added wrapping, and have mutliple lines especially good for a message. The last thing is to have to user not being able to read messages.
Check out my app. It's on my profile here.