Binding.Source 속성

정의

바인딩 소스로 사용할 개체를 가져오거나 설정합니다.

public:
 property System::Object ^ Source { System::Object ^ get(); void set(System::Object ^ value); };
public object Source { get; set; }
member this.Source : obj with get, set
Public Property Source As Object

속성 값

바인딩 소스로 사용할 개체입니다.

예제

다음 예제에서는 첫 번째 강조 표시된 줄과 같이 네임스페이스에 정의된 라는 PersonName 문자열 속성이 있는 SDKSample 개체를 사용합니다Person. 요소를 포함하는 <src> 강조 표시된 줄에서 속성 값Joe이 인 개체를 PersonName 인스턴스화합니다Person. 이 작업은 Resources 섹션에서 수행되며 x:Key가 할당됩니다.

<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>

요소를 포함하는 강조 표시된 줄은 속성에 <TextBlock> 바인딩하는 PersonName 방법을 보여 줍니다. 결과적으로 컨트롤이 TextBlock "Joe" 값으로 표시됩니다.

다음 예제에서는 개체의 Source 값이 Binding 속성 Application.Current으로 static 설정됩니다.

<ComboBox.IsEnabled>
    <MultiBinding Converter="{StaticResource specialFeaturesConverter}">
        <Binding Path="CurrentUser.Rating" 
          Source="{x:Static Application.Current}"/>
        <Binding Path="CurrentUser.MemberSince" 
    Source="{x:Static Application.Current}"/>
    </MultiBinding>
</ComboBox.IsEnabled>

전체 예제는 데이터 바인딩 데모를 참조하세요.

설명

기본적으로 바인딩은 속성이 설정된 경우 속성에 DataContext 지정된 데이터 컨텍스트를 상속합니다. 그러나 Source 속성은 의 원본을 명시적으로 설정하고 상속된 데이터 컨텍스트를 재정의 Binding 할 수 있는 방법 중 하나입니다. 여러 속성이 같은 데이터 컨텍스트를 상속받는 범위를 설정하는 기능이 필요하지 않은 경우 DataContext 속성 대신 Source 속성을 사용할 수 있습니다.

Binding.ElementNameBinding.RelativeSource 속성을 사용하면 바인딩의 원본을 명시적으로 설정할 수도 있습니다. 그러나 각 바인딩에 대해 세 가지 속성 , ElementNameSource, 및 RelativeSource중 하나만 설정해야 합니다. 그렇지 않으면 충돌이 발생할 수 있습니다. 이 속성은 바인딩 소스 충돌이 있는 경우 예외를 throw합니다.

이 속성을 지우려면 으로 DependencyProperty.UnsetValue설정합니다.

XAML 특성 사용

<object Source="object"/>  

XAML 값

object
기존 개체입니다. 기존 개체를 참조하려면 StaticResource 태그 확장을 사용합니다.

적용 대상