How To Create and Reference a Custom Control Library
This guide shows you how to create a custom control library and reference it for use in an Avalonia UI app.
Creating a custom control library
Creating a new class library project
To start, you need a class library project in which to collect your custom control files.
- Rider
- Visual Studio
- Go to File → New Solution. Alternatively, Add → New Project to add the class library as a new project within an existing solution.
- In the left panel, under the section "Project Type", select Class Library.
- Name the project, e.g. "CCLibrary".
- For "Target framework", select the preferred .NET version.
- Click Create.

- Go to File → New → Project/Solution.
- Select .NET Class Library as the project template. Use the search bar to locate this template if it does not appear on the suggested list.
- Name the project, e.g. "CCLibrary".
- For "Target framework", select the preferred .NET version.
- Click Create.

Installing Avalonia in the class library project
Next, you must install the Avalonia NuGet package in the class library.
- Rider
- Visual Studio
- In the solution panel, select your class library project.
- Click Tools → NuGet → Manage NuGet Packages.
- Search for "Avalonia" in the search bar.
- Select Avalonia.
- Select the preferred version.
- Click the name of your class library project at the bottom of the panel to install Avalonia to that project.

- In the solution explorer, select your class library project.
- Click Project → Manage NuGet Packages.
- Go to the Browse tab. Search for "Avalonia".
- Select Avalonia.
- Select the preferred version.
- Click Install.

Adding a custom control to the class library
Now that your class library is set up, you can start adding custom controls to it.
In this example, we'll create a simple custom control named MyCustomControl, a blank box that can be filled with a colour of your choice.
- In the class library project, create a new
.csfile. - Create the custom control as shown below.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
namespace CCLibrary
{
/// <summary>A custom control that renders a coloured rectangle.</summary>
public class MyCustomControl : Control
{
/// <remarks>
/// Defines a styled property for the background colour, which can be set in XAML.<br/>
/// AddOwner reuses the existing BackgroundProperty from Border.<br/>
/// </remarks>
public static readonly StyledProperty<IBrush?> BackgroundProperty =
Border.BackgroundProperty.AddOwner<MyCustomControl>();
/// <summary>Gets and sets the colour used to fill the rectangle.</summary>
public IBrush? Background
{
get { return GetValue(BackgroundProperty); }
set { SetValue(BackgroundProperty, value); }
}
/// <summary>Renders the control.</summary>
public sealed override void Render(DrawingContext context)
{
if (Background != null) // Only render if a colour is set.
{
var renderSize = Bounds.Size; // Get the size of the control.
context.FillRectangle(Background, new Rect(renderSize)); // Fill the rectangle with the colour.
}
base.Render(context);
}
}
}
You can continue adding as many custom controls to the library as you wish.