Search code examples
wpfxamllayouttextblock

XAML Layout with two columns


Can the below layout be achieved more efficiently than specifying a 13x2 Grid?

WPF Layout

Is there for example anything where TextBlock would have a property "heading" that is shown before its Content? Like the String "Date taken:" would be the property of the 1st TextBlock. Then, I would achieve the above layout without a need to specify a 2-column Grid, like below:

<StackPanel Orientation="Vertical">
    <TextBlock Text="Specify date taken" Heading="Date taken:" />
    ...
    <TextBlock />
</StackPanel>

Solution

  • You can use a 13x2 Grid quite safely here. The layout algorithm of a Grid with only Auto height rows is actually not as expensive as you might think; it becomes heavier when mixing Auto, fixed and star heights. However in your situation it would be the most logical approach.

    If you do encounter performance issues you should guarantee that the height of your input boxes is always fixed and equal to the height of your text labels, then you can use a StackPanel.

    If you want to do your own thing with the headers on the TextBlock you should create a user control as specified with the Header and Text properties. However you need to create it for all the input controls you have (DatePicker, TextBox etc etc) this can be done with templating.