Search code examples
asp.net-corerazorblazor

check if RenderFragment is empty


Is there a way to check if RenderFragment is empty?

For example, in the code below, MyComp shows the Detail if Open is true & you can toggle Open by clicking on the header. Now If there's no header, I'd like the Detail fragment to always be open. This would be easy if there was a property like HeaderTitle.IsEmpty.

    <MyComp Open="false">
        <HeaderTitle>
             @if (!String.IsNullOrEmpty(hdr)) {
                 ...
                <div class="flex-grow"> @hdr </div>
            }
        </HeaderTitle>
        <Detail>
            ...
        </Detail>
   </MyComp>

Edit

For further discussion I added a feature request here.


Solution

  • Old question, but as it is unanswered and getting upvotes....

    A RenderFragment is a code method, not a container, so it does not have an Empty state. It can be null - which is equivalent to empty in the sense that it will produce no rendered output.

    In the context of this question, you can effectively treat null as Empty - and simply have a method in your code

    bool HeaderTitleIsEmpty => HeaderTitle is null;