Getting Started : Chapter 7 MFC and WPF Interoperability : XAML
XAML
The XAML View loads controls from a XAML file. The root of the XAML file cannot be a Window or a Page. Instead, the file must start with one of the existing WPF layout types: Canvas, DockPanel, Grid, StackPanel, or WrapPanel. Delegate function names are not defined in the XAML; rather, they are defined in code as shown in the section “Delegate Mapping”.
XAML Class To create an XAML View, derive from the SFLWPFXAMLViewEx class. See the sample file <InstallDir>\Samples\FoundationEx .NET\WPFMDIEx\WPFMDIExXAMLView.h for an example.
Loading XAML Load the XAML using the SFLXAMLDocumentEx function LoadXAML(). You can call this function in the derived document class’s OnNewDocument() function or in the OnInitialUpdate() call of the corresponding View class. For example:

CXAMLDoc* pDoc = (CXAMLDoc*)GetDocument();
if(pDoc) pDoc->LoadXAML(“XamlWindow.xaml”);
Delegate Mapping
Delegates for WPF controls are mapped using the BEGIN_DELEGATE_MAP() and END_DELEGATE_MAP() macros. Between the begin and end macros, the EVENT_DELEGATE_ENTRY() macro is used to map delegate functions to function names in the XAML file.
For example:
 
// Header
public:
BEGIN_DELEGATE_MAP( CWPFMDIExXAMLView )
EVENT_DELEGATE_ENTRY(button1_Click, Object^, RoutedEventArgs^)
EVENT_DELEGATE_ENTRY(button2_Click, Object^, RoutedEventArgs^)
END_DELEGATE_MAP()
public:
void button1_Click(Object^ sender, RoutedEventArgs^ e);
void button2_Click(Object^ sender, RoutedEventArgs^ e);
 
Creating XAML Controls in Code Behind
Visual Studio contains no facility for automatic code generation for creating WPF controls in C++/CLI. Controls that correspond to the control labels in XAML are easily set up using the macros SFLEX_WPF_CREATE_CTRL_FROM_VIEW() and SFLEX_WPF_ASSIGN_DELEGATE(). For example:
 
SFLEX_WPF_CREATE_CTRL_FROM_VIEW(Button, btn1, "button1")
SFLEX_WPF_ASSIGN_DELEGATE(btn1, Click, button1_Click)
 
SFLEX_WPF_CREATE_CTRL_FROM_VIEW(Button, btn2, "button2")
SFLEX_WPF_ASSIGN_DELEGATE(btn2, Click, button2_Click)