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

屬性值

要作為繫結來源的物件。

範例

下列範例會使用 Person 物件搭配名為 PersonName 的字串屬性,該屬性定義于 命名空間中 SDKSample ,如第一個反白顯示的行所示。 在包含 <src> 專案的反白顯示行中,它會具 PersonName 現化 Person 屬性值為 Joe 的物件。 這會在 區 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 會設定為 static 屬性 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>

如需完整範例,請參閱 資料系結示範

備註

根據預設,如果已設定系結,系結會繼承 屬性所 DataContext 指定的資料內容。 不過, Source 屬性是您可以明確設定 來源的其中一 Binding 種方式,並覆寫繼承的資料內容。 如果您不需要建立範圍的功能,其中數個屬性會繼承相同的資料內容,您可以使用 Source 屬性,而不是 DataContext 屬性。

Binding.ElementNameBinding.RelativeSource 屬性也可讓您明確設定系結的來源。 不過,每個系結只能設定三個屬性之 ElementName 一 、 SourceRelativeSource ,否則可能會發生衝突。 如果系結來源衝突,這個屬性會擲回例外狀況。

若要清除此屬性,請將它設定為 DependencyProperty.UnsetValue

XAML Attribute Usage

<object Source="object"/>  

XAML 值

object
現有的 物件。 若要參考現有的物件,請使用 StaticResource 標記延伸

適用於