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 強調表示された行が示すように、 名前空間で定義されている という名前 PersonNameSDKSample 文字列プロパティを持つ オブジェクトを使用します。 要素を含む<src>強調表示された行で、 プロパティ値Joeを使用して Person オブジェクトをPersonNameインスタンス化します。 これは 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.Currentstatic設定されています。

<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 1 つです。 複数のプロパティが同じデータ コンテキストを継承するスコープを確立する機能が不要な場合は、DataContext プロパティの代わりに Source プロパティを使用できます。

Binding.ElementNameプロパティと Binding.RelativeSource プロパティを使用すると、バインディングのソースを明示的に設定することもできます。 ただし、バインディングごとに 、、ElementNameSourceおよび RelativeSourceの 3 つのプロパティのうち 1 つだけを設定する必要があります。または、競合が発生する可能性があります。 バインディング ソースの競合がある場合、このプロパティは例外をスローします。

このプロパティをクリアするには、 に設定します DependencyProperty.UnsetValue

XAML 属性の使用方法

<object Source="object"/>  

XAML 値

object
既存のオブジェクト。 既存のオブジェクトを参照するには、StaticResource マークアップ拡張機能を使用します

適用対象