FrameworkElement.DataContext 屬性

定義

取得或設定 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}"/>

屬性值

Object

Platform::Object

IInspectable

用做為資料內容的物件。

範例

本範例會將 DataCoNtext 直接設定為自訂類別的實例。

如果您使用 C++/WinRT{Binding} 標記延伸,則會使用 FrameworkElement::D ataCoNtext 屬性和 BindableAttribute。 如果您使用 {x:Bind} 標記延伸,則不會使用 FrameworkElement::D ataCoNtextBindableAttribute

如需下列 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;
' Create an instance of the MyColors class 
' that implements INotifyPropertyChanged. 
Dim textcolor As 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中的索引鍵資源結合,然後在 XAML 中使用 {StaticResource} 標記延伸 參考來設定 DataCoNtext。

另一種設定 DataCoNtext 的技術是將它新增至執行時間物件樹狀結構的根目錄,做為應用程式初始化邏輯的一部分,就在呼叫 InitializeComponent之後。 這項技術會顯示在 資料系結概觀中。

除了指定來源之外,資料內容也可以儲存系結宣告的其他特性,例如資料來源的路徑。

設定 DataCoNtext 方便將相同物件上不同屬性的數個系結設定為共用資料內容。 不過,它適用于未定義的 DataCoNtext,而且所有必要系結限定條件都存在於個別的系結語句中。

實作物件資料來源的方式會根據您的需求和程式設計語言而有所不同。 如需詳細資訊,請參閱深入了解資料繫結

C# 和 Microsoft Visual Basic 資料內容的常見案例是使用支援變更通知的 CLR 定義商務物件。 對於商務物件,做為資料內容的自訂類別通常會實作 INotifyPropertyChanged,讓資料的更新可以更新單向或雙向系結。 如果資料來源是商務物件的集合,它可以實作 INotifyCollectionChanged 加清單支援 (IListList) ,或衍生自 ObservableCollection

適用於

另請參閱