Skip to main content
A newer version of this page is available. .

LayoutControlItem.Control Property

Gets or sets the control which is owned by the layout item.

Namespace: DevExpress.XtraLayout

Assembly: DevExpress.XtraLayout.v19.1.dll

Declaration

[DefaultValue(null)]
[DXCategory("Behavior")]
public virtual Control Control { get; set; }

Property Value

Type Default Description
Control *null*

A Control descendant which is owned by the layout item.

Remarks

When a control is assigned to the LayoutControlItem.Control property, it is automatically added to the LayoutControl.Controls collection.

Do not change the bound control’s settings that affect its visibility and position using the properties this control provides (for instance, Location, Parent, Size, Visible and Enabled). Use the properties provided by the LayoutControlItem class instead.

After a control has been assigned to the LayoutControlItem.Control property, this property can only be changed by wrapping your code with the LayoutControl.BeginUpdate and LayoutControl.EndUpdate methods.

When an item is disposed of at runtime, its control is not destroyed.

Note

To allow a Layout Control’s layout to be customized and serialized, you need to ensure that Name properties of the layout items and controls within the layout items are set to unique values. You must set the Name properties to unique values for layout items and controls that are created at runtime. The control’s Name property must be initialized before this control is assigned to the LayoutControlItem.Control property.

Example

The following example shows how to change a layout item’s control in code.

Assume that a DataLayoutControl is bound to a data source containing a Description field. The DataLayoutControl.RetrieveFields method retrieves information on available fields in the data source, and automatically creates layout items with controls for these fields. By default, for a field of the string type, a layout item with a TextEdit control inside it is created. The example shows how to locate a layout item with a control bound to the Description field, and replace its TextEdit control with a MemoEdit control. To change a layout item’s control, the new control must be assigned to the LayoutControlItem.Control property within the LayoutControl.BeginUpdate and LayoutControl.EndUpdate method pair.

using DevExpress.XtraLayout;
using DevExpress.XtraEditors;

dataLayoutControl1.RetrieveFields();

foreach (BaseLayoutItem baseItem in dataLayoutControl1.Items) {
    LayoutControlItem item = baseItem as LayoutControlItem;
    if(item !=null && item.Control.DataBindings.Count > 0)
        if (item.Control.DataBindings[0].BindingMemberInfo.BindingField == "Description") {
            dataLayoutControl1.BeginUpdate();
            Control prevControl = item.Control;
            Binding binding = prevControl.DataBindings[0];
            prevControl.DataBindings.Clear();
            dataLayoutControl1.Controls.Remove(prevControl);
            Control newControl = new MemoEdit();
            newControl.Name = "myMemoEdit";
            // Bind the new control to the same field as the previous control.
            newControl.DataBindings.Add(new Binding(binding.PropertyName, binding.DataSource,
                binding.BindingMemberInfo.BindingField, binding.FormattingEnabled));
            dataLayoutControl1.Controls.Add(newControl);
            item.Control = newControl;                        
            prevControl.Dispose();
            dataLayoutControl1.EndUpdate();
            // Change the item's size after the EndUpdate method.
            item.Size = new Size(100, 50);
            break;
        }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the Control property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also