skip to Main Content

cannot set the style property of an element it’s always empty. When I write out StyleString it says color: red

[Transposed verbatim from the comment]
AT(StyleString = ShouldBeError ? "color: red" : "") 
</div>
 @StyleString 
<MudCard> 
  <MudCardHeader> 
    <CardHeaderContent> 
      <MudText Typo="Typo.h6" Style="AT($"{StyleString}")"> 
       //..... 

ATcode { 
//... 
private bool ShouldBeError; 
private string StyleString = ""; 
private List<Location> Data = new List<Location>(); 

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I put a div above MudText and it worked straight away.


  2. I’m not a MudBlazor user, so here’s a very simple example demonstrating how to set properties on any element/component. In this case the class of the button rather than the style on a component.

    @page "/"
    
    <PageTitle>Index</PageTitle>
    
    <h1>Hello, world!</h1>
    
    Welcome to your new app.
    
    <div class="m-2">
        <button class="@_btnCss" @onclick="this.Change">
            Switch
        </button>
    </div>
    
    <SurveyPrompt Title="How is Blazor working for you?" />
    
    @code {
        private bool _enabled;
        private string _btnCss => _enabled ? "btn btn-success" : "btn  btn-secondary";
    
        private void Change()
            => _enabled = !_enabled;
    }
    

    [Personal] Note that I try to keep component logic out of the markup whenever I can. I think it’s easier to read.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search