Binding.Source 属性

定义

获取或设置绑定的数据源。

public:
 property Platform::Object ^ Source { Platform::Object ^ get(); void set(Platform::Object ^ value); };
IInspectable Source();

void Source(IInspectable value);
public object Source { get; set; }
var object = binding.source;
binding.source = object;
Public Property Source As Object
<Binding Source="sourceReference"/>

属性值

Object

Platform::Object

IInspectable

包含绑定数据的源对象。

示例

下面的代码示例演示如何在 XAML 中设置此属性。 有关完整的代码列表,请参阅 XAML 数据绑定示例

<StackPanel>

  <StackPanel.Resources>
    <CollectionViewSource x:Name="teamsCVS"/>
  </StackPanel.Resources>

  <ListBox x:Name="lbTeams" Height="200" 
    ItemsSource="{Binding Source={StaticResource teamsCVS}}">
    <ListBox.ItemTemplate>
      <DataTemplate><!-- ... --></DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>

</StackPanel>

有关示例演示如何在代码中设置此属性,请参阅 Binding 类。

注解

Binding 对象上的 Source 属性是可选的。 如果在 Binding 对象上设置了 Source 属性,则数据源仅适用于使用该 Binding 对象的目标属性。

若要创建由树中的所有子元素继承的数据源,请改为在父元素上设置 DataContext 属性。 然后,父元素及其所有子元素将 DataContext 作为其绑定的源。 如果为子元素设置了 Source,它将覆盖该实例中的 DataContext 继承。

如果路径为空或路径定义的 Source 对象的属性,则目标可以直接绑定到 Source 对象。 路径是在 XAML 中使用绑定语法设置的,或者在创建 Binding 对象时设置。

绑定已附加到目标元素和目标属性后,无法设置 Binding 对象的属性值。 如果尝试此操作,则会出现运行时异常。

适用于

另请参阅