WinForms HTML & CSS Templates — Alert Control

WinForms Team Blog
20 April 2022

As you may already know, you can create two types of notifications/alerts with our WinForms product line (via the DevExpress AlertControl and ToastNotificationManager). Until recently, we recommended use of the DevExpress ToastNotificationManager to display native Windows 10+ notifications.

Despite somewhat limited customization options (toasts only offer nine layout templates), we felt that the ToastNotificationManager represented a better and more modern alternative to our traditional panel-based AlertControl.

This recommendation may change once we release HTML & CSS templates for our AlertControl. These web-inspired templates should allow you to create stylish notifications while leveraging the simplicity of the AlertControl itself. The figure below illustrates some of the sample templates available in our Demo Center (find and copy templates you like to your own projects)

Most notifications are simply a rectangle with a couple of text blocks, images, and buttons. Designing such simple objects should be relatively easy for everyone – whether you have experience with HTML & CSS or are beginning to leverage its potential inside your WinForms app. For example, the following template (included in the Demo Center) creates a notification with an icon, title, description, and "OK" button.

<div class="container">
    <div class="popup">
		<img src="${SvgImage}" class="image" />
    	<div class="caption">Notification Title</div>
    	<div class="text">This notification uses a web-inspired template.</div>
		<div id="okButton" class="ok-button">OK</div>
	</div>
</div>

Note that in this sample markup, notification title and description are static strings. This solution works just fine if you have a single message to display for your users.

Of course, our data binding feature offers more flexibility — you can create a single template and pass different data objects to it. As such, you may reuse a single template for multiple notification messages.

If you prefer this option, use the ${Data_property_name} placeholder as follows:

<div class="text">${Caption}</div>
<div class="text">${Text}</div>

"Caption" and "Text" are standard placeholders that can be replaced directly via AlertControl.Show overloads:

alertControl1.Show(this, "Sample caption", "Sample notification text");

You can add as many placeholders as your template design requires, but remember to handle the AlertControl.BeforeFormShow event and pass a data object to the e.HtmlPopup.DataContext property. For example, the code below uses a div element to display a string combined from five placeholders: two for string values (FullName, Ticker), two for numbers (Percentage, Price), and one for custom Direction enum values.

<div class="message-text">
    ${FullName} ({Ticker}) {Direction} {Percentage}%. The current price is ${Price}.
</div>

The notification image is also retrieved at runtime, the img tag uses a placeholder instead of a static src property value.

<img src="${StockImage}" class="message-image" />

This sample app utilizes StockInfo class objects as data items.

public class StockInfo {
    public StockInfo(string ticker, string fullName, Direction direction,
        double percentage, double price, SvgImage img) {
        Ticker = ticker;
        FullName = fullName;
        Direction = direction;
        Percentage = percentage;
        Price = price;
        StockImage = img;
    }

    public string Ticker { get; set; }
    public  string FullName { get; set; }
    public Direction Direction { get; set; }
    public double Percentage { get; set; }
    public double Price { get; set; }
    public SvgImage StockImage { get; set; }
}

public enum Direction {
    [Description("rises")]
    Up,
    [Description("falls")]
    Down
}

Notifications are triggered when a data item's "Price" value changes significantly over a short period of time. The corresponding item is assigned to the e.HtmlPopup.DataContext property in the AlertControl.BeforeFormShow event handler.

void AlertControl1_BeforeFormShow(object sender, AlertFormEventArgs e) {
    // TODO: Retrieve a data item
    e.HtmlPopup.DataContext = myStockInfoInstance;
}

As a result, a notification retrieves data for its ${Field_Name} placeholders from a data item assigned as a DataContext. Note that the color of the side strip changes based on the "Direction" enumeration value.

Tell Us What You Think

It's been some time since our initial release of HTML & CSS template support for our WinForms Data Grid and WinForms Scheduler (as well as our stand-alone HtmlContentControl and HtmlContentPopup components). If you've already tried our HTML & CSS template engine, please tell us your thoughts and share suggestions so we can adjust our product roadmap accordingly.

Free DevExpress Products – Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We’ll be happy to follow-up.
dbSoft
dbSoft
Perfect!! Thank you Devexpress. Keep growing html - css styles!!
20 April 2022
nkaykut
Köksal
Thank you DevExpress. You are doing a good job.
20 April 2022
dbSoft
dbSoft
I couldn't find it in Demo Center. Does this function has been released in last update? 
20 April 2022
Dmitry (DevExpress)
Dmitry Korovin (DevExpress)
@dbSoft It will be released in v22.1, the post is just a sneak peek into the feature to collect initial thoughts and suggestions. Once released, you'll be able to find this demo under "Win | HTML & CSS Templates | Alert Control".
20 April 2022
Konstantin Balashov
Konstantin Balashov

Thank you DevExpress!

Waiting for text selection in HTML templates ... 

20 April 2022
Mahfoud Bouabdallah
Mahfoud Bouabdallah

Thanks DevExpress

How about XtraMessageBox does will be get these changes (HTML & CSS) 

20 April 2022
Dmitry (DevExpress)
Dmitry Korovin (DevExpress)
@Mahfoud we have templated messages in mind, should be a logical next step after alert windows.
21 April 2022
Christopher Jay
Christopher Jay
FANTASTIC.  The Windows 10 native notifications were too limited in styles so we stuck with the AlertControl all of these years.  Finally, I will be able to make them look more modern and customize them more.  Thank you so much!  Looking forward to the templated message boxes, too!  Maybe an InputBox as well?
22 April 2022
danch
danch

I'v been waiting for a new version of DevExpress.Maui.XXX...

The official CollectionView can support multi row and multi column at the same time to display, hope the DXCollectionView can support grid layout display.  Just like:

<CollectionView.ItemsLayout>

    <GridItemsLayout Orientation="Vertical" HorizontalItemSpacing="15" VerticalItemSpacing="5" Span="5"/>

22 April 2022


When will the new DevExpress.Maui version be released?

23 April 2022
Dennis (DevExpress)
Dennis Garavsky (DevExpress)
@danch: Thank you for your feedback on the collection view control - our team will take it into account. We expect to publish .NET MAUI RC1-compatible packages within a week or so.

25 April 2022
Uwe Keim
Uwe Keim

Really do love the direction this goes.

When I finally can design my whole WinForms app with HTML/CSS-like syntax, a later migration to Blazor/Core Pages would be much easier.

25 April 2022
Juan Bosco Rodriguez Martinez
Juan Bosco Rodríguez
Where the CSS is defined?
2 May 2022
Dmitry (DevExpress)
Dmitry Korovin (DevExpress)
@Juan In the same window you enter HTML, this designer window pops up when you click an ellipsis button (…) next to the the HtmlTemplate property in the Visual Studio Property Grid. Please have a look at this overview article: https://docs.devexpress.com/WindowsForms/403397/common-features/html-css-based-desktop-ui
2 May 2022

Please login or register to post comments.