--- title: "How to: Create a Binding in Code" description: Learn how to create a binding in code in a Windows Presentation Foundation application by calling the SetBinding method directly. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "binding data [WPF], creating" - "data binding [WPF], creating" ms.assetid: 1a606db9-cf5f-42ed-a1c5-9e4722ec77a0 --- # How to: Create a Binding in Code This example shows how to create and set a in code. ## Example The class and the class both expose a `SetBinding` method. If you are binding an element that inherits either of these classes, you can call the method directly. The following example creates a class named, `MyData`, which contains a property named `MyDataProperty`. [!code-csharp[CodeOnlyBinding#DataObject](~/samples/snippets/csharp/VS_Snippets_Wpf/CodeOnlyBinding/CSharp/MyData.cs#dataobject)] [!code-vb[CodeOnlyBinding#DataObject](~/samples/snippets/visualbasic/VS_Snippets_Wpf/CodeOnlyBinding/VisualBasic/MyData.vb#dataobject)] The following example shows how to create a binding object to set the source of the binding. The example uses to bind the property of `myText`, which is a control, to `MyDataProperty`. [!code-csharp[CodeOnlyBinding#1](~/samples/snippets/csharp/VS_Snippets_Wpf/CodeOnlyBinding/CSharp/binding.cs#1)] [!code-vb[CodeOnlyBinding#1](~/samples/snippets/visualbasic/VS_Snippets_Wpf/CodeOnlyBinding/VisualBasic/App.vb#1)] For the complete code sample, see [Code-only Binding Sample](/previous-versions/dotnet/netframework-3.5/ms771500(v=vs.90)). Instead of calling , you can use the static method of the class. The following example, calls instead of to bind `myText` to `myDataProperty`. [!code-csharp[CodeOnlyBinding#BOSetBinding](~/samples/snippets/csharp/VS_Snippets_Wpf/CodeOnlyBinding/CSharp/binding.cs#bosetbinding)] [!code-vb[CodeOnlyBinding#BOSetBinding](~/samples/snippets/visualbasic/VS_Snippets_Wpf/CodeOnlyBinding/VisualBasic/App.vb#bosetbinding)] ## See also - [Data Binding Overview](/dotnet/desktop-wpf/data/data-binding-overview) - [How-to Topics](data-binding-how-to-topics.md)