I want to set defaults for parameters on a third-party component. Say I have this:
myBasePage.cs:
public class MyBasePage : ComponentBase
{
public IEnumerable MyData { get; set; }
}
myPage.razor:
@inherits MyBasePage
<ThirdPartyComponent Data="@MyData" />
Since Data on ThirdPartyComponent is a [Parameter] with a DataHasChanged() virtual method, by rendering the blazor component like that, I'll get one-way binding, and if I change MyData on my page, programmatically, the component will update. This will work fine.
Now, say I can't modify ThirdPartyComponent, but I want to make some defaults in it based on my base page... like so:
myPage.razor:
@inherits MyBasePage
<MyDerivedComponent PageComponent="@this" />
myDerivedComponent.cs:
public class MyDerivedComponent : ThirdPartyComponent
{
[Parameter] public MyBasePage PageComponent { get; set; }
public override void OnInitialized()
{
/* Set other parameter defaults */
this.OtherParameter = 10;
/* Bind to Data, as if I was passing it as a parameter in the Razor template */
this.Data = PageComponent.MyData;
}
}
This line:
this.Data = PageComponent.MyData;
Doesn't create a binding at all (and if I modify MyData, the blazor component doesn't get updated). Is there any way to programmatically create it?
Note: the real ThirdPartyComponent includes not only tons of parameters but also templates, etc. For many reasons, I'd like MyDerivedComponent to be of a derived type, and not a "parent component" with a child of ThirdPartyComponent, if that's possible at all).
Data="@MyData"will make the page re-render wheneverMyDatachanges. Do you change its value as a response to some events? - Pharaz FadaeiStateHasChangedor it's anEventCallback. If I set it programatically (not on the razor component template), it does neither. - Jcl<MyDerivedComponent Data="@this.Data">, it works as expected... if I set it via code (<MyDerivedComponent PageComponent="@this">, thenthis.Data = PageComponent.Data;), it doesn't - JclData="@this.Data"and then change the value ofthis.Datathe component will automatically re-render? - Pharaz FadaeiStateHasChangedonMyBasePagewhenever the value ofDatachanges, theMyBasePagecomponent will rerender accordingly.MyDerivedComponentis a child ofMyBasePageand has a complex-typed parameter (PageComponent) so itsOnParametersSetwill be called based on the docs. - Pharaz Fadaei