views:

34

answers:

3

I simple form which have some fields. So i decided to add few DataField with textboxes and labels. Labels manageable and their length can be changed and i need to place my textboxes on the same distance from left side. How can i do this thing?

alt text

+1  A: 

You can use a grid

<Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"/>
    <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Label Content="Label" Grid.Row="0" Grid.Column="0"/>
    <TextBox Grid.Row="0" Grid.Column="1"/>
    <Label Content="Long Label" Grid.Row="1" Grid.Column="0"/>
    <TextBox Grid.Row="1" Grid.Column="1"/>

NVM
Datafields have additional functionality which is very useful for me. So i need solution with them
Evgeny
Aah I didnt see that! HiTech Magic's answer sounds like the right way to go then.
NVM
+1  A: 

The sort of alignment and grouping of controls you mention indicates you really need to use a DataGrid control. The column widths are then managed for you and extensive databinding options are available for lists of arbitrary objects (in this case objects with say DisplayLabel and DisplayText properties).

If you do not like the appearance of the standard datagrid the templating will let you make it look like a form instead (remove headings, grid-line row highlighting etc).

Hope this helps.

Enough already
+1  A: 

I decided to add control with next logic:

  void MyControl_LayoutUpdated(object sender, EventArgs e)
  {
            if (this.columnSeparator.ActualWidth!=0&&this.columnSeparator.ActualWidth != this.columnSeparator.MinWidth)
            {
                this.IsLoaded = true;
                SetWidth();
            }
  }

 private void SetWidth()
        {
            if (IsWidthSet)
                return;
            if (!this.IsLoaded)
                return;
            var parentPanel = this.Parent as Panel;
            if (parentPanel != null)
            {
                var textFields = parentPanel.Children.Where(p => p is BpTextField).Cast<BpTextField>().ToList();
                double max = this.LabelWidth;
                foreach (var textField in textFields)
                {
                    max = Math.Max(max, textField.LabelWidth);
                    if (!textField.IsLoaded)
                        return;
                }

                foreach (var textField in textFields)
                {
                    textField.LabelWidth = max;
                }

                this.LabelWidth = max;
            }
        }
        public bool IsLoaded { get; set; }
        public bool IsWidthSet { get; set; }

Now all labels aligned and well.

Evgeny