I have a lot of XAML that is in a complete mess and it's usually due to one thing. GRID
, Grid.Column
and Grid.Row
craziness!!
Now theres a few common ways you can make labelled lists in XAML:.
With a <Grid>
Advantages: Autosizing width of columns and rows
Disadvantages: No easy way to insert rows/columns either in VS or Blend [STILL!!], potentially messy XAML if you're not careful, tonnes of attached properties, margins need to be set on each control, grid is meant for everything and definitely not designed for this purpose!
Yes I know there are handy tools to insert rows and columns without having to manually renumber all the fields such as XAML Powertools (see video). But its still clumsy in my opinion (despite being life saver at the same time.)
<Grid Grid.Row="6" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- Name -->
<TextBlock Text="Name:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Name="txtName" Grid.Row="0" Grid.Column="1" Width="100" Margin="5,5,0,0"/>
<!-- Address -->
<TextBlock Text="Address:" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Name="txtAddress" Grid.Row="1" Grid.Column="1" Width="100" Margin="5,5,0,0"/>
</Grid>
With nested <Stackpanel> controls
(This is what I always seem to end up using)
Advantages: Very easy to reorder items, relatively clean XAML
Disadvantages: Need to know the width of the label column and set it on each item, still need to mess with margins
<StackPanel>
<!-- Name -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:" Width="60" VerticalAlignment="Center" TextAlignment="Right"/>
<TextBox Name="txtName" Width="100" Margin="5,5,0,0"/>
</StackPanel>
<!-- Address -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Address:" Width="60" VerticalAlignment="Center" TextAlignment="Right"/>
<TextBox Name="txtAddress" Grid.Row="1" Grid.Column="1" Width="100" Margin="5,5,0,0"/>
</StackPanel>
</StackPanel>
With a DataForm
Advantages: Handles editing, different views for different edit states etc.
Disadvantages: Still in preview quality band, too heavy for most of the time you need a simple list of items
<dataform:DataForm x:Name="dataForm" Width="350" ItemsSource="{Binding}" HorizontalAlignment="Left" MaxWidth="500" Margin="4" Grid.Column="1"/>
See http://www.silverlightshow.net/items/Creating-Rich-Data-Forms-in-Silverlight-3-Customization.aspx
What I want!
I'd like some kind of grid that still works in Blend but allows me to set the text of the label easily with an attached property - or whatever the 'best' XAML way is. Allowing for multiple columns would be great but probably a lot more complicated.
Advantages: Easy to reorder items, clean XAML, set 'cellpadding' in one place
Disadvantages: None yet!
<LabelledGrid CellPadding="5" LabelStyle="{StaticResource labelStyle}>
<TextBox LabelledGrid.Label="Name" Name="txtName" />
<TextBox LabelledGrid.Label="Address" Name="txtAddress" />
</LabelledGrid>
or maybe
<LabelledGrid CellPadding="5" LabelStyle="{StaticResource labelStyle}>
<LabelledGrid.GridItem>
<LabelledGrid.Label>
Name:
</LabelledGrid.Label>
<TextBox Name="txtName" />
</LabelledGrid.GridItem>
<LabelledGrid.GridItem>
<LabelledGrid.Label>
Address:
</LabelledGrid.Label>
<TextBox Name="txtAddress" />
</LabelledGrid.GridItem>
</LabelledGrid>
Are there any existing controls like how I'm describing LabelledGrid
- or how should I go about creating this?