FrameworkElement.DataContext プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
FrameworkElement のデータ コンテキストを取得または設定します。 データ コンテキストの一般的な用途は、 FrameworkElement が {Binding} マークアップ拡張を使用し、データ バインディングに参加する場合です。
public:
property Platform::Object ^ DataContext { Platform::Object ^ get(); void set(Platform::Object ^ value); };
IInspectable DataContext();
void DataContext(IInspectable value);
public object DataContext { get; set; }
var object = frameworkElement.dataContext;
frameworkElement.dataContext = object;
Public Property DataContext As Object
<frameworkElement DataContext="binding"/>
- or -
<frameworkElement DataContext="{StaticResource keyedObject}"/>
プロパティ値
データ コンテキストとして使用するオブジェクト。
例
次の使用例は、 DataContext をカスタム クラスのインスタンスに直接設定します。
C++/WinRT と {Binding} マークアップ拡張を使用している場合は、FrameworkElement::DataContext プロパティと BindableAttribute を使用します。
{x:Bind} マークアップ拡張機能を使用している場合、FrameworkElement::DataContextもBindableAttributeも使用しません。
以下の C++/WinRT コード例の背景 (たとえば、 .idl ファイルの一覧の使用方法、生成する実装ファイルの処理方法) については、 XAML コントロールと C++/WinRT プロパティへのバインドに関するページを参照してください。
// MyColors.idl
namespace MyColorsApp
{
[bindable]
[default_interface]
runtimeclass MyColors : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
MyColors();
Windows.UI.Xaml.Media.SolidColorBrush Brush1;
}
}
// MyColors.h
#pragma once
#include "MyColors.g.h"
namespace winrt::MyColorsApp::implementation
{
struct MyColors : MyColorsT<MyColors>
{
MyColors() = default;
Windows::UI::Xaml::Media::SolidColorBrush Brush1();
void Brush1(Windows::UI::Xaml::Media::SolidColorBrush const& value);
winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
void PropertyChanged(winrt::event_token const& token) noexcept;
private:
Windows::UI::Xaml::Media::SolidColorBrush m_brush1{ nullptr };
winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
};
}
namespace winrt::MyColorsApp::factory_implementation
{
struct MyColors : MyColorsT<MyColors, implementation::MyColors>
{
};
}
// MyColors.cpp
#include "pch.h"
#include "MyColors.h"
namespace winrt::MyColorsApp::implementation
{
Windows::UI::Xaml::Media::SolidColorBrush MyColors::Brush1()
{
return m_brush1;
}
void MyColors::Brush1(Windows::UI::Xaml::Media::SolidColorBrush const& value)
{
if (m_brush1 != value)
{
m_brush1 = value;
m_propertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"Brush1" });
}
}
winrt::event_token MyColors::PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return m_propertyChanged.add(handler);
}
void MyColors::PropertyChanged(winrt::event_token const& token) noexcept
{
m_propertyChanged.remove(token);
}
}
<!-- MainPage.xaml-->
...
<TextBox x:Name="MyTextBox" Background="{Binding Brush1}"/>
...
// MainPage.h
...
#include "MyColors.h"
#include "MainPage.g.h"
...
// MainPage.cpp
#include "pch.h"
#include "MainPage.h"
using namespace winrt;
using namespace Windows::UI;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Media;
namespace winrt::MyColorsApp::implementation
{
MainPage::MainPage()
{
InitializeComponent();
// Create an instance of the MyColors class
// which implements INotifyPropertyChanged.
winrt::MyColorsApp::MyColors textcolor{ winrt::make<winrt::MyColorsApp::implementation::MyColors>() };
// Set the Brush1 property value to a new SolidColorBrush
// with the color Red.
textcolor.Brush1(SolidColorBrush(Colors::Red()));
// Set the DataContext of the TextBox named MyTextBox.
MyTextBox().DataContext(textcolor);
}
...
}
// Create an instance of the MyColors class
// that implements INotifyPropertyChanged.
MyColors textcolor = new MyColors();
// Brush1 is set to be a SolidColorBrush with the value Red.
textcolor.Brush1 = new SolidColorBrush(Colors.Red);
// Set the DataContext of the TextBox MyTextBox.
MyTextBox.DataContext = textcolor;
注釈
データ コンテキスト は、オブジェクトリレーションシップ階層内の連続する親オブジェクトからオブジェクトがデータ バインディング情報を継承できる概念です。
データ コンテキストの最も重要な側面は、データ バインディングに使用されるデータ ソースです。
DataContextの一般的な用途は、データ ソース オブジェクトに直接設定することです。 このデータ ソースは、ビジネス オブジェクトなどのクラスのインスタンスである可能性があります。 または、データ コンテキストがバッキング コレクションの変更を検出できるように、データ ソースを監視可能なコレクションとして作成することもできます。 データ ソースがプロジェクトにも含まれるライブラリによって定義されている場合、DataContextの設定は、多くの場合、ResourceDictionary でキー付きリソースとしてデータ ソースをインスタンス化し、{StaticResource} マークアップ拡張参照を使用して XAML でDataContextを設定する方法と組み合わされます。
DataContextを設定するもう 1 つの手法は、InitializeComponentを呼び出した直後に、アプリ初期化ロジックの一部としてランタイム オブジェクト ツリーのルートに追加することです。 この手法は、 データ バインディングの概要に示されています。
データ コンテキストでは、ソースを指定するだけでなく、データ ソースへのパスなど、バインディング宣言の追加の特性を格納することもできます。
DataContextの設定は、同じオブジェクト上の異なるプロパティの複数のバインドを共有データ コンテキストに設定する場合に便利です。 ただし、 DataContext が未定義であることと、必要なすべてのバインディング修飾が別々のバインド ステートメントに存在する場合に有効です。
オブジェクト データ ソースの実装方法は、要件とプログラミング言語によって異なります。 詳細については、「データ バインディングの詳細」を参照してください。
C# データ コンテキストの一般的なシナリオは、変更通知をサポートする CLR で定義されたビジネス オブジェクトを使用することです。 ビジネス オブジェクトの場合、データ コンテキストとして使用されるカスタム クラスは通常 、INotifyPropertyChanged を実装するため、データの更新で一方向または双方向のバインディングを更新できます。 データ ソースがビジネス オブジェクトのコレクションである場合は、 INotifyCollectionChanged とリストサポート (IList<T> または List<T>) を実装するか 、ObservableCollection<T> から派生させることができます。
適用対象
こちらもご覧ください
- Binding
- データ バインディングの詳細
ObservableCollection