Binding.Source 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
바인딩 소스로 사용할 개체를 가져오거나 설정합니다.
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
속성 값
바인딩 소스로 사용할 개체입니다.
예제
다음 예제에서는 첫 번째 강조 표시된 줄과 같이 네임스페이스에 SDKSample
정의된 문자열 PersonName
속성이 있는 개체를 사용합니다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.ElementName 및 Binding.RelativeSource 속성을 사용하면 바인딩의 원본을 명시적으로 설정할 수도 있습니다. 그러나 각 바인딩에 대해 세 가지 속성 , ElementNameSource및 RelativeSource, 중 하나만 설정해야 합니다. 그렇지 않으면 충돌이 발생할 수 있습니다. 바인딩 소스 충돌이 있는 경우 이 속성은 예외를 throw합니다.
이 속성을 지우려면 으로 DependencyProperty.UnsetValue설정합니다.
XAML 특성 사용
<object Source="object"/>
XAML 값
object
기존 개체입니다. 기존 개체를 참조하려면 StaticResource 태그 확장을 사용합니다.