How to change classes of a div element using Blazor

后端 未结 3 1109
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 05:20

I have a typical div element in a cshtml page in the form:

@(ErrorMessage)
3条回答
  •  无人共我
    2021-02-07 05:43

    For complete freaks (me) create a razor component (xaml lover dream):

    Example usage

    
    
     
    
    
    

    Result: divs with an appropriate class applied, class='size-small' for Size="Small".

    You can add whatever parameter to such component and change classes inside upon your logic, keeping your main page clean.

    ContentView.razor

    @if (IsVisible)
        {
        
    @ChildContent
    } @code { [Parameter] public RenderFragment ChildContent { get; set; } [Parameter] public bool IsVisible { get; set; } = true; [Parameter] public string Size { get { return _size.ToString(); } set { _size = Enum.Parse(value); switch (_size) { case MySize.Big: sizeClass = "size-big"; break; case MySize.Small: sizeClass = "size-small"; break; default: sizeClass = ""; break; } } } private MySize _size; private string sizeClass; public enum MySize { Default, Small, Big } }

提交回复
热议问题