Compartir a través de


How to: Create a Simple Binding

This example shows you how to create a simple Binding.

Example

In this example, you have a Person object with a string property named PersonName. The Person object is defined in the namespace called SDKSample.

The following example instantiates the Person object with a PersonName property value of Joe. This is done in the Resources section and assigned an x:Key.

<Window Name="root"
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:SDKSample"
  xmlns:system="clr-namespace:System;assembly=mscorlib"
  SizeToContent="WidthAndHeight"
  Title="Simple Data Binding Sample">

  <Window.Resources>
    <src:Person x:Key="myDataSource" PersonName="Joe"/>

    ...

  </Window.Resources>

    ...

</Window>

To bind to the PersonName property you would do the following:

<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>

As a result, the TextBlock appears with the value "Joe".

For the complete sample, see Simple Binding Sample.

See Also

Concepts

Data Binding Overview

Other Resources

Data Binding How-to Topics