Share via


How to: Create an ElementName Data Binding

You can bind an element's property value to a property on another named element by using the WPF Designer for Visual Studio. For example, you can bind a control's Background property to the Background property of its parent element.

To create a data binding to a named element

  1. Create a new WPF Application project in Visual Basic or Visual C# named DataBindingDemo. For more information, see How to: Create a New WPF Application Project.

    MainWindow.xaml opens in the WPF Designer. 

  2. In Design view, right-click the root Grid control and select Properties from the shortcut menu.

  3. At the top of the Properties window, click in the Name box, type rootGrid, and press Enter to name the Grid control.

  4. In the Properties window, locate the Background property.

  5. Use the brush editor to create and assign a brush to the Background property. For more information, see How to: Create a Brush by Using the Brush Editor.

  6. From the Toolbox, drag a ListBox control into the Grid control.

  7. In the Properties window, locate the ListBox control's Background property.

  8. Click the Inheritance property marker (property marker inheritance icon).

  9. Click Apply Data Binding.

    The data binding builder appears.

  10. On the Source pane in the left panel, click ElementName.

  11. In the middle panel, click rootGrid.

    Source pane of data binding builder

  12. Click the Path pane.

  13. On the Path pane in the left panel, click the Background property.

    The ListBox control's Background property is bound to the grid control's Background property.

    Path pane of data binding builder

  14. Click outside the data binding builder to close the builder.

  15. In XAML view, note the markup extension that was added for the Background property.

    Background="{Binding ElementName=rootGrid, Path=Background}"

See Also

Reference

Binding

Concepts

Data Binding Overview

Other Resources

Working with the WPF Designer