Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent default in Blazor?

I have this simple button in MudBlazor:

<MudIconButton Class="answer-button" Icon="@_icon" OnClick="OnClick" Color="@_color" 
                   @oncontextmenu="OnContextMenu"
                   @oncontextmenu:preventDefault="true"/>

I'm just trying to prevent right click action on button and apply my own logic to it.

I was using this article, but I get this build error

EventCalendarAnswer.razor(5,36): error RZ10010: The component parameter 'oncontextmenu' is used two or more times for this component. Paramete
rs must be unique (case-insensitive). The component parameter 'oncontextmenu' is generated by the '@oncontextmenu:preventDefault' directive attribute

How can I prevent default behaviour in Blazor?

like image 200
Morasiu Avatar asked Dec 14 '25 13:12

Morasiu


1 Answers

You can wrap the MudIconButton inside a div and apply the @oncontextmenu attribute to the parent element.

<div @oncontextmenu="OnContextMenu" @oncontextmenu:preventDefault="true">
    <MudIconButton Class="answer-button" Icon="@_icon" OnClick="OnClick" Color="@_color" />
</div>
like image 179
Dimitris Maragkos Avatar answered Dec 16 '25 04:12

Dimitris Maragkos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!