In my real life example I am at a layout stage and before I actually move any further I would like to review my current approach. I am still learning about UI, XAML on WPF technology and I guess I haven't gotten my head around all the possible approaches yet.
I have built this very simple SSCCE to better demonstrate what I mean:
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Width="200" MinWidth="150" SizeToContent="Height">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0">First Name</Label>
<Label Grid.Row="1" Grid.Column="0">Last Name</Label>
<Label Grid.Row="2" Grid.Column="0">Address</Label>
<Label Grid.Row="3" Grid.Column="0">Phone</Label>
<TextBox Grid.Row="0" Grid.Column="1"></TextBox>
<TextBox Grid.Row="1" Grid.Column="1"></TextBox>
<TextBox Grid.Row="2" Grid.Column="1" Height="Auto" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
<TextBox Grid.Row="3" Grid.Column="1"></TextBox>
</Grid>
</Window>
Here is what it looks like:
Assume I will be having a list of 50+ "rows" in a form that will be either populated by a User or deserialized from an XML. At some point I may be asked to insert an extra row in between the current elements.
I can already see a potential problem of having to adjust the Grid.Row
attribute's value at the point of inserting a new item until the last one.
For example, I am inserting a new element before the current Grid.Row
3 which would require me to manually shift all the attached properties of Grid.Row
on all the remaining rows (say 50 - 3 ). At this point it just does not sound like I have had a well designed window/xaml from the start.
How can I overcome the problem of adding extra rows to this grid? Maybe I should start over with a different Content Control?
I am not looking for a complete solution just some insights and tips as I'm having so much fun actually digging and implementing based on tips rather than copy-pasting others code.
My alternative suggestion:
I have imported the xmlns:sys="clr-namespace:System;assembly=mscorlib"
and was thinking of using a List
and then filling each ListBoxItem
with Labels
and Textbox
as well as other controls like Grid
etc but I don't think I can use more than 1 control inside the <ListBoxItem>
therefore I am not sure this is a good approach.
What I do like about it is the syntax of inserting:
<ListBox>
<ListBoxItem>
<!-- first item and no need to explicitly numerate -->
</ListBoxItem>
<ListBoxItem>
<!-- second item -->
</ListBoxItem>
<ListBoxItem>
<!-- newly inserted becomes 3rd -->
</ListBoxItem>
<ListBoxItem>
<!-- was third but now is 4th -->
</ListBoxItem>
</ListBox>
Another approach I am considering is
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label>boo</Label>
<TextBox Grid.Column="1"></TextBox>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label>foo</Label>
<TextBox Grid.Column="1"></TextBox>
</Grid>
</StackPanel>
As it would rather be easy to insert new items and no explicit numeration required, but this is kind of a lot of code as for each StackPanels' item I would need to create a <Grid>
with Rows and Columns definitions.