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> 元素的突出显示行中,它使用 JoePersonName 属性值实例化 Person 对象。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".

在下面的示例中,Binding 对象的 Source 值设置为 static 属性 Application.CurrentIn 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.ElementName" 和 "Binding.RelativeSource" 属性,还可以显式设置绑定的源。The Binding.ElementName and Binding.RelativeSource properties also enable you to set the source of the binding explicitly. 但是,只应为每个绑定设置三个属性中的一个(ElementNameSourceRelativeSource),否则可能会发生冲突。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.UnsetValueTo 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

适用于