Binding.Source 属性

定义

获取或设置要用作绑定源的对象。Gets or sets the object to use as the 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

属性值

要用作绑定源的对象。The object to use as the binding source.

示例

下面的示例使用Person一个对象, 该对象具有SDKSample命名PersonName空间中定义的名为的字符串属性, 因为第一个突出显示的行所示。The following example uses a Person object with a string property named PersonName that is defined in the SDKSample namespace, as the first highlighted line shows. 在包含<src>元素的突出显示行中, 它将Person对象PersonName的属性值Joe实例化。In the highlighted line that contains the <src> element, it instantiates the Person object with a PersonName property value of Joe. 该操作在 Resources 部分完成,并指定了 x:KeyThis is done in the Resources section and assigned an 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到属性。The highlighted line that contains the <TextBlock> elements shows how to bind to the PersonName property. 因此, TextBlock控件将显示值 "Joe"。As a result, the TextBlock control appears with the value "Joe".

在下面的示例中, Source Binding static对象的值设置为属性Application.Current:In the following example, the Source values of the Binding objects are set to the static property Application.Current:

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

有关完整示例, 请参阅数据绑定演示For the full example, see Data Binding Demo.

注解

默认情况下, 绑定继承DataContext属性指定的数据上下文 (如果已设置)。By default, bindings inherit the data context specified by the DataContext property, if one has been set. 但是, Source属性是您显式设置的源Binding和替代继承的数据上下文的方法之一。However, the Source property is one of the ways you can explicitly set the source of a Binding and override the inherited data context. 如果不需要特定功能(用于建立其中有多个属性继承相同的数据上下文的作用域),则可以使用 Source 属性而不是 DataContext 属性。If you do not need the functionality of establishing a scope in which several properties inherit the same data context, you can use the Source property instead of the DataContext property.

使用Binding.ElementNameBinding.RelativeSource属性, 还可以显式设置绑定的源。The Binding.ElementName and Binding.RelativeSource properties also enable you to set the source of the binding explicitly. 但是, 只应为每个绑定设置ElementNameSource个属性RelativeSource、、和中的一个, 否则可能会发生冲突。However, only one of the three properties, ElementName, Source, and RelativeSource, should be set for each binding, or a conflict can occur. 如果存在绑定源冲突, 则此属性将引发异常。This property throws an exception if there is a binding source conflict.

若要清除此属性, 请将DependencyProperty.UnsetValue其设置为。To clear this property, set it to DependencyProperty.UnsetValue.

XAML 属性用法XAML Attribute Usage

<object Source="object"/>  

XAML 值XAML Values

objectobject
现有的对象。An existing object. 若要引用现有对象, 请使用StaticResource 标记扩展To refer to an existing object, use the StaticResource Markup Extension

适用于