Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

@context variable in implicit child content

$
0
0

I made a simple list component that works with INotifyCollectionChanged:

@using System.Collections.Specialized@typeparam TItem@if (Items != null && ChildComponent != null){    @foreach (var item in Items)    {        @ChildComponent(item);    }}@code {    [Parameter]    public IList<TItem>? Items { get; set; }    [Parameter]    public RenderFragment<TItem>? ChildComponent { get; set; }    protected override void OnParametersSet()    {        if (ReferenceEquals(_previousItems, Items))        {            return;        }        if (_previousItems is INotifyCollectionChanged previousNotifyCollectionChanged)        {            previousNotifyCollectionChanged.CollectionChanged -= OnCollectionChanged;        }        if (Items is INotifyCollectionChanged notifyCollectionChanged)        {            notifyCollectionChanged.CollectionChanged += OnCollectionChanged;        }        _previousItems = Items;    }    private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs eventArgs)    {        StateHasChanged();    }    private IList<TItem>? _previousItems;}

When I use this component and explicitly define the child content, I can use @context to access the properties of each item:

<ListComponent Items="ViewModel.Items"><ChildComponent><option>@context.Name</option></ChildComponent></ListComponent>

However, when I try to define the child content implicitly, I get an error:

<ListComponent Items="ViewModel.Items"><option>@context.Name</option></ListComponent>
error CS0103: The name 'context' does not exist in the current context

Examples such as TemplateColumn (Source) from quickgrid seem to support it though, so I am unsure what I am doing wrong. How to I get access to the @context variable from implicit child content?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>