Home > Blockchain >  how to make wraper around component with bind-value / type param ? on mudblazor mudselect
how to make wraper around component with bind-value / type param ? on mudblazor mudselect

Time:10-07

i need to build some wraper around mudblazor compoent

so lets say that i have basic exapmle like:

            <MudSelect T="int" @bind-Value="@model.dict1" For="@(() => @model.dict1)" AnchorOrigin="Origin.BottomCenter">                
                    <MudSelectItem T="int"  Value="1">1</MudSelectItem>
                    <MudSelectItem T="int" Value="2">2</MudSelectItem>
            </MudSelect>

so now i want to do some wraper around this like

<DictMudSelectComponent T="int" @bind-Value="@model.dict1" For="@(() => model.dict1)" TableName="test" AnchorOrigin="Origin.BottomCenter"></DictMudSelectComponent>

so based on parameter TableName i want to render diferent items in select list

for now i have:

DictMudSelectComponent.razor

@typeparam T
<MudSelect T="@T" @bind-Value="@Value" For="()=>For()" @attributes=AllOtherAttributes>

<MudSelectItem T="int"  Value="1">1</MudSelectItem>
<MudSelectItem T="int"  Value="2">2</MudSelectItem>
<MudSelectItem T="int"  Value="3">3</MudSelectItem>
@code {
 private Func<T> _for ;

[Parameter]
public Func<T> For
{
    get => _for;
    set
    {
        if (_for == value)
            return;
        _for = value;
    }
}

private T _value ;

[Parameter]
public T Value
{
    get => _value;
    set => _value = value;
}

[Parameter]
public string TableName { get; set; } = "";


[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> AllOtherAttributes { get; set; } = new();

}

but even this does not work - why ? it compiles fine but on render i get

Unhandled exception rendering component: Unable to set property 'AnchorOrigin' on object of type 'MudBlazor.MudSelect`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. The error was: Specified cast is not valid.
System.InvalidOperationException: Unable to set property 'AnchorOrigin' on object of type 'MudBlazor.MudSelect`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. The error was: Specified cast is not valid.
 ---> System.InvalidCastException: Specified cast is not valid.
   at Microsoft.AspNetCore.Components.Reflection.PropertySetter.CallPropertySetter[MudSelect`1,Origin](Action`2 setter, Object target, Object value)
   at Microsoft.AspNetCore.Components.Reflection.PropertySetter.SetValue(Object target, Object value)
   at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value)
   at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
   at Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target)
   at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
   at MudBlazor.MudBaseInput`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<>n__1(ParameterView parameters)
   at MudBlazor.MudBaseInput`1.<SetParametersAsync>d__177[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()

what am i misisng here? thanks and regards

CodePudding user response:

okay so the first issue is that it needs to be

<MudSelect  @bind-Value="@Value"  For="@For" >

and in @code

private Expression<Func<TypeParam>>? _for;

[Parameter]
public Expression<Func<TypeParam>> For
{
    get => _for!;
    set
    {
        if (_for == value)
            return;
        _for = value;
    }
}

then it runs without errors but im unable to change value - it stays on 0 /default value forever - why? i canot chose this hardcoded fornow 1/2/3 - always goes back to 0/default any advise ? regads

edit ok second issue is that

 [Parameter]
 public T? Value
 {
     get => _value;
     set 
     { 
         if (!Equals(value, _value))
         {
             _value = value;
             if (ValueChanged.HasDelegate)
                 ValueChanged.InvokeAsync(_value);
         }
     }
 }

this have to be checked if equal because if not then the problem is that MudSelect is triggering the Setter on a Parameter, which then Invokes the callback, which then invokes the Setter on the Parameter again.

  • Related