Nuts
Nuts

Reputation: 2813

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>

Upvotes: 0

Views: 1108

Answers (2)

Bas
Bas

Reputation: 27085

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.

Upvotes: 1

Fernando Urkijo
Fernando Urkijo

Reputation: 3506

Try this:

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

You can apply custom styles to each Run tag. Hope it helps!

Upvotes: 0

Related Questions