DEV Community

Cover image for MudBlazor List Items: How To Create Awesome Blazor List Views
Dev Leader
Dev Leader

Posted on • Originally published at devleader.ca

MudBlazor List Items: How To Create Awesome Blazor List Views

MudBlazor List Items: How To Create Awesome Blazor List Views appeared first on Dev Leader.

I’m trying to spend more time in Blazor, especially as I build out my next side project. Historically, I’ve spent a lot of time writing WinForms and WPF applications, but over the past few years, I’ve been mostly removed from any user interface development. With the rise of Blazor, I feel like I have another opportunity to jump back into things! As I dive a bit deeper, I’ve found MudBlazor has a great set of UI components — especially as I started building using the MudBlazor list items!

In this article, I’ll share a quick overview of some of the ways that you can use MudBlazor list items, which will focus on MudList and MudListItem components. Check out the quick code tips and see if MudBlazor might be a fit for you!


Overview of MudBlazor and List Components

MudBlazor is a popular UI component library for Blazor development that provides a rich set of ready-to-use components. It simplifies the process of creating responsive and customizable user interfaces in Blazor applications.

Coming from a WinForms and WPF background, I find I need to spend additional time getting familiar with the Blazor component libraries we have access to. Once you get a level of comfort, creating more powerful UIs feels a lot less restrictive!

MudList is a key component in MudBlazor that allows us to create lists in our Blazor applications. MudList provides a flexible and configurable way to render list items.

MudListItems are the individual items within a MudList. They allow for customization and configuration, enabling us to define the appearance and behavior of each list item according to our specific requirements. We’ll see some code examples coming up that allow us to customize the look and feel of MudListItems to get things the way we want!

    <MudList>
        <MudListItem>Item 1</MudListItem>
        <MudListItem>Item 2</MudListItem>
        <MudListItem>Item 3</MudListItem>
        <MudListItem>Item 4</MudListItem>
    </MudList>
Enter fullscreen mode Exit fullscreen mode

Using MudBlazor’s MudList and MudListItems components, we can create dynamic and interactive list views in Blazor. MudBlazor takes care of the complexities of handling the rendering and responsiveness, allowing us to focus on the important stuff — building good Blazor apps.


Configuring List Items in MudBlazor

MudBlazor provides a powerful set of components for configuring list items, allowing you to customize their appearance and behavior to meet your specific needs. In this section, we’ll check these out by seeing how to add icons, text, and additional components to enhance your list view in Blazor.

Adding Icons to MudBlazor List Items

MudBlazor offers a wide range of icons that you can easily integrate into your list items. To add an icon, use the Icon property of the MudListItem component and provide the name of the desired icon. For example:

    <MudListItem Icon="Icons.Filled.CheckCircle" Text="Completed" />
Enter fullscreen mode Exit fullscreen mode

I haven’t yet had a need for icons, but I know that when it comes to adding polish to a UI it can be really helpful. Especially when it comes to making functionality easier to identify and more accessible.

Customizing Text and Content:

You can customize the text displayed in a list item by setting the Text property. Additionally, MudBlazor allows you to include additional content within a list item using the ChildContent property. For instance:

    <MudListItem>
         <MudIcon Icon="Icons.Filled.Speaker" />
         <span>Notification</span>
         <MudBadge>5</MudBadge>
    </MudListItem>
Enter fullscreen mode Exit fullscreen mode

In the above example, the list item contains an icon, a text span, and a badge component. This allows for a more visually appealing and informative list item. You can continue to nest all sorts of things in here! For one of my more recent UI screens, I’m working on a more complex item layout that requires a grid and multiple components within the MudItem.

Responding to Events:

MudBlazor provides event handlers that allow you to respond to user interactions with list items. For instance, you can handle the OnClick event to perform an action when a list item is clicked:

    <MudListItem OnClick="e => OnListItemClick(e)">
         <MudIcon Icon="Icons.Filled.Speaker" />
         <span>Notification</span>
         <MudBadge>5</MudBadge>
    </MudListItem>
Enter fullscreen mode Exit fullscreen mode

In the above example, the OnListItemClick method in your code-behind or component will be called when the list item is clicked. Here we can see the code for the handler:

    @code {
        private async Task OnListItemClick(MouseEventArgs e)
        {
            // Handle the click event here
            Console.WriteLine("ListItem clicked!");

            // You can also implement other logic here, 
            // such as navigation or updating UI elements.

            // And... we can use async await!
            await Task.Yield();
        }
    }
Enter fullscreen mode Exit fullscreen mode

Writing async event handlers this way is a nice touch compared to some of the nastiness we’re required to do traditionally with async event handlers. And even though this is much nicer, I’ll have a follow-up article where I had problems getting this to fire because of some RenderMode issues conflicting with my dependency injection paradigm!


Wrapping Up MudBlazor List Items

As I spend more time working in Blazor, I’m trying to build out more familiarity so I can be as proficient as I used to be with WPF and WinForms. That’s going to take some time. I think it’s important to get familiar with the awesome control libraries we have access to, and MudBlazor is a great fit so far.

It’s extremely common for us as application developers to need to show data in lists, and we need to do so in a way that’s obvious to users. MudBlazor has the MudList with MudListItems, and this is a highly configurable way for us to do *just* that.

If you found this useful and you’re looking for more learning opportunities, consider subscribing to my free weekly software engineering newsletter and check out my free videos on YouTube! Meet other like-minded software engineers and join my Discord community!


Want More Dev Leader Content?

  • Follow along on this platform if you haven’t already!
  • Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos: SUBSCRIBE FOR FREE
  • Looking for courses? Check out my offerings: VIEW COURSES
  • E-Books & other resources: VIEW RESOURCES
  • Watch hundreds of full-length videos on my YouTube channel: VISIT CHANNEL
  • Visit my website for hundreds of articles on various software engineering topics (including code snippets): VISIT WEBSITE
  • Check out the repository with many code examples from my articles and videos on GitHub: VIEW REPOSITORY

Dev Leader Weekly | Substack

My weekly newsletter that simplifies software engineering for you, along with C# code examples. Join thousands of software engineers from companies like Microsoft and Amazon who are already reading! Click to read Dev Leader Weekly, a Substack publication with thousands of subscribers.

favicon weekly.devleader.ca

Top comments (2)

Collapse
 
aheisleycook_14 profile image
Austin heisley cook

Is mud better then asp

Collapse
 
devleader profile image
Dev Leader

I have never used Vanilla ASP (I assume you mean like MVC, right?) to create user interfaces for web applications. However, Blazor is pretty awesome, I just needed some additional control over what's available by default. So far, MudBlazor has been a great library to leverage!