How to: Import a Namespace into XAML
To use your custom controls and third-party controls in XAML, you need to import namespaces and reference assemblies. For more information, see XAML Namespaces and Namespace Mapping for WPF XAML.
Poznámka
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.
Importing a local namespace in XAML
Create a new WPF Application project named "DemoApplication". For more information, see How to: Create a New WPF Application Project.
Add a new User Control (WPF) item named "DemoControl.xaml" to the DemoApplication project. For more information, see How to: Add New Items to a WPF Project.
On the Build menu, select Build Solution to build the solution.
Open MainWindow.xaml in the designer.
In XAML view, in the opening Window tag, insert a new line following the second xmlns mapping.
Type xmlns:dc= and select DemoApplication in assembly DemoApplication from the IntelliSense list.
The designer inserts a namespace mapping for the DemoApplication namespace.
<Window x:Class="DemoApplication.MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:dc="clr-namespace:DemoApplication" Title="MainWindow" Height="300" Width="300"> <Grid> </Grid> </Window>
After the opening tag of the Grid element, type <dc: and select DemoControl from the IntelliSense list.
Type a closing bracket /> to close the element.
The Grid element should look like the following:
<Grid> <dc:DemoControl /> </Grid>
Importing a third-party namespace in XAML
Add a new WPF User Control Library project named "VendorControlLibrary" to the DemoApplication solution. When the DemoApplication solution is built, an assembly is created for each project in the solution. For more information, see How to: Create a WPF UserControl Library Project.
In the DemoApplication project, add a project reference to the VendorControlLibrary project. For more information, see How to: Add or Remove References in Visual Studio.
On the Build menu, select Build Solution to build the solution.
Open MainWindow.xaml in the designer.
In XAML view, in the opening Window tag, insert a new line following the third xmlns mapping.
Type xmlns:vc= and select VendorControlLibrary in assembly VendorControlLibrary from the IntelliSense list.
Intellisense inserts a namespace mapping for the VendorControlLibrary namespace, which is defined in the VendorControlLibrary.dll assembly.
<Window x:Class="DemoApplication.MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:dc="clr-namespace:DemoApplication" xmlns:vc="clr-namespace:VendorControlLibrary;assembly=VendorControlLibrary" Title="MainWindow" Height="300" Width="300"> <Grid> <dc:DemoControl /> </Grid> </Window>
After the closing tag of the DemoControl element, type <vc: and select UserControl1 from the IntelliSense list.
Type a closing bracket /> to close the element.
The Grid element should look like the following:
<Grid> <dc:DemoControl /> <vc:UserControl1 /> </Grid>
See Also
Concepts
XAML Namespaces and Namespace Mapping for WPF XAML