Inheritance
It sounds like you most likely want to @inherit the MudBlazor component in a new component of your own. This essentially allows you to use the MudBlazor component as a base class for your new class/component, avoiding having to redefine all of the parameters that the MudBlazor component already defines.
This can be done in C# or razor. Here's a razor example, OutlinedMudButton.razor
, that allows the user to still set all of the usual MudButton parameters, except for the Variant
parameter:
@inhertis MudButton
@code {
// Hide the MudButton's Variant parameter so it can not be changed by users.
protected new Variant Variant { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
base.Variant = Variant.Outlined;
}
}
This blog post shows a complete example of how to inherit a component to extend it, change default parameter settings, add new UI elements to it, hide/disable parameters of the base component, and potentially change its functionality. If you want to add new UI elements around the component, you can use the following line of code to display the base component that you are inheriting from, placing the new UI elements around it.
@{
// Display the base component that we are inheriting from.
base.BuildRenderTree(__builder);
}
Composition
Depending on your use case, you may want to go with composition instead of inheritance, by creating a new component that simply contains an instance of the MudBlazor component that you want to decorate. This may be easier or more appropriate, especially if you want to hide many of the MudBlazor component's parameters so that users are not able to change them. Be aware that if you use attribute splatting as suggested in other answers/comments, the downside is your component will not provide editor intellisense for all of the MudBlazor component's parameters.
Here is another razor example, OutlinedMudButton.razor
, that creates a component with an Outlined MudButton where the only thing the user can set is the button Text
:
<MudButton Variant="Variant.Outlined">@Text</MudButton>
@code {
[Parameter]
public string Text { get; set;}
}