Nasıl yapılır: Basit bir Bağlama Oluşturma

Bu örnek, basit bir oluşturma hakkında size bir örnek Binding gösterir.

Örnek

Bu örnekte adlı dize Person özelliğine sahip bir nesne PersonName var. nesnesi Person adlı ad alanı içinde SDKSample tanımlanır.

Aşağıdaki örnekte öğesini içeren <src> vurgulanan satır, nesnesinin bir özellik Person değeriyle PersonName örneğini Joe oluşturur. Bu, bölümünde yapılır Resources ve bir x:Key atanır.

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:SDKSample"
  SizeToContent="WidthAndHeight"
  Title="Simple Data Binding Sample">

  <Window.Resources>
    <src:Person x:Key="myDataSource" PersonName="Joe"/>
    <Style TargetType="{x:Type Label}">
      <Setter Property="DockPanel.Dock" Value="Top"/>
      <Setter Property="FontSize" Value="12"/>
    </Style>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="Width" Value="100"/>
      <Setter Property="Height" Value="25"/>
      <Setter Property="DockPanel.Dock" Value="Top"/>
    </Style>
    <Style TargetType="{x:Type TextBlock}">
      <Setter Property="Width" Value="100"/>
      <Setter Property="Height" Value="25"/>
      <Setter Property="DockPanel.Dock" Value="Top"/>
      <Setter Property="Padding" Value="3"/>
    </Style>
  </Window.Resources>
  <Border Margin="5" BorderBrush="Aqua" BorderThickness="1" Padding="8" CornerRadius="3">
    <DockPanel Width="200" Height="100" Margin="35">
      <Label>Enter a Name:</Label>
      <TextBox>
        <TextBox.Text>
          <Binding Source="{StaticResource myDataSource}" Path="PersonName"
                   UpdateSourceTrigger="PropertyChanged"/>
        </TextBox.Text>
      </TextBox>
      
      <Label>The name you entered:</Label>
      <TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
    </DockPanel>
  </Border>
</Window>

Öğesini içeren vurgulanan <TextBlock> satır, denetimi TextBlock özelliğine PersonName bağlar. Sonuç olarak , TextBlock değeri "Ali" ile birlikte görüntülenir.

Ayrıca bkz.