C
C#4d ago
jim.jam.1

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!
No description
No description
No description
18 Replies
ACiDCA7
ACiDCA74d ago
@jim.jam.1 did you try debugging it? i guess that datacontext isnt set yet when the ctor of mainwindow is called
Jdizzle
Jdizzle4d ago
DataContext=“{property}” In the picture you have no bindings . Just create a binding.
jim.jam.1
jim.jam.1OP4d ago
Sorry, didn't include that part in the original screenshot for some reason:
No description
Jdizzle
Jdizzle4d ago
Your items control will make rows automatically Could use a stack panel and add a gap in your template Spacing I mean
jim.jam.1
jim.jam.1OP4d ago
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?
Jdizzle
Jdizzle4d ago
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.
jim.jam.1
jim.jam.1OP4d ago
For some reason it's still not consuming vertical space
No description
No description
Jdizzle
Jdizzle3d ago
put Itemscontrol add proptery for HorizontialAlignment= Strech you can add to the StackPanel Spacing="n" too
jim.jam.1
jim.jam.1OP3d ago
I've added Horiontal and Vertical alignments = "Stretch" to the ItemsControl however it hasn't actually done anything. UI design always loses me...
jim.jam.1
jim.jam.1OP3d ago
For reference:
No description
Jdizzle
Jdizzle3d ago
<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>
No description
Jdizzle
Jdizzle3d ago
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
Jdizzle
Jdizzle3d ago
No description
Jdizzle
Jdizzle3d ago
<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>
jim.jam.1
jim.jam.1OP3d ago
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?
Jdizzle
Jdizzle3d ago
Then you do a MaxHeight on the Text Blocks MinHeight i believe Hold on I do something...
Jdizzle
Jdizzle3d ago
<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>
No description
Jdizzle
Jdizzle3d ago
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.

Did you find this page helpful?