語言

FrameworkElement.DataContext 屬性

定義

取得或設定 FrameworkElement 的資料上下文。 資料上下文的常見用途是 a 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}"/>

屬性值

Object

Platform::Object

IInspectable

作為資料上下文的物件。

範例

這個範例直接將 設定 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 用途是直接將其設定為資料來源物件。 這個資料來源可能是某個類別的實例,例如商業物件。 或者你可以建立一個可觀察的資料庫,讓資料上下文能偵測到後備集合的變更。 如果資料來源是由專案中包含的函式庫定義,通常會將 a DataContext 與在 ResourceDictionary 中實例化為有鍵的資源,然後在 XAML 中以 {StaticResource} 標記延伸引用設定 。DataContext

另一種設定 DataContext 方法是將它加入執行時物件樹的根節點,作為應用程式初始化邏輯的一部分,在呼叫 InitializeComponent之後。 此技術在 資料綁定概述中有展示。

除了指定來源外,資料上下文還能儲存綁定宣告的其他特性,例如進入資料來源的路徑。

設置 a DataContext 方便於在同一物件上將多個不同屬性的綁定設定到共享的資料上下文。 然而,a DataContext 未定義且所有必要的具約束性限定存在於獨立的具約束性陳述中是有效的。

你如何實作物件資料來源,會依需求和程式語言而異。 如需詳細資訊,請參閱 深入數據綁定。

C# 資料情境的常見情境是使用 CLR 定義的業務物件,支援變更通知。 對於商業物件,用作資料上下文的自訂類別通常實作 INotifyPropertyChanged,讓資料的更新能更新單向或雙向綁定。 若資料來源為商業物件集合,則可實作 INotifyCollectionChanged 及清單支援(IList<T 或>List<T>),或源自 ObservableCollection<T>。

適用於

另請參閱