6

Every time someone asks me to provide an interface to input values, I find myself creating grids with a lot of rows and columns. Tried to look for an answer to this question but find it hard

Code

 <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
            <Grid.Resources>
                <Style TargetType="RowDefinition">
                    <Setter Property="Height" Value="25"/>
                </Style>
                <Style TargetType="TextBox">
                    <Setter Property="Margin" Value="2"/>
                </Style>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="150"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0">Input 1</TextBlock>
            <TextBox Grid.Row="0" Grid.Column="1"></TextBox>
            <TextBlock Grid.Row="1" Grid.Column="0">Input 2</TextBlock>
            <TextBox Grid.Row="1" Grid.Column="1"></TextBox>
        </Grid>

Result

Example

Questions that come to mind are if there are any performance considerations or if there is another method that removes the need of maintaining attributes with row and column numbers.

The option of creating a custom control with label and input is discussed here however that would introduce more overhead.

Creating the layout seems somewhat repetitive which makes me wonder if there is a tool for exactly this.

Question

Is this the most optimal way of doing it? If not, why and how should it be done?

6
  • 2
    I can tell you that using a Label is heavier than using a TextBlock. Besides that I don't think you would have performance problems using a grid. joshsmithonwpf.wordpress.com/2007/07/04/… I see that you changed your code to use textblock instead of label. Very nice!
    – TMan
    Apr 30, 2014 at 13:41
  • Thank you. Editted to keep focus in the question.
    – RB84
    Apr 30, 2014 at 13:44
  • I personally would use ListView, which is using GridView with defined CellTemplate which could consist of 1. TextBlock with Text set to the property name using Reflection and TextBox which would set the Property on object that you're working on. This approach would make the design in somewhat a little easier.
    – XAMlMAX
    Apr 30, 2014 at 13:57
  • Have you looked at data grid controls? Apr 30, 2014 at 14:03
  • 1
    I also use reflection to create the Xaml.
    – Gayot Fow
    Apr 30, 2014 at 17:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.