I have a Blazor SfMultiSelect inside a custom control I created. I want to pass to my custom control a list of items which should be rendered as "checked" in the SfMultiSelect when my custom control renders.
So, in my custom control I exposed a property to receive the list of items which should be "checked".
Inside my custom control, my SfMultiSelect was coded as follows:
<SfMultiSelect @ref="DDLDocumentos" ID="ddlDocumentos" TValue="List<TipoDocumentoDistribuicaoVM>" TItem="TipoDocumentoDistribuicaoVM" AllowFiltering="true" Placeholder="Selecione os Documentos..." Mode="@VisualMode.CheckBox"
DataSource="@LstTiposDocumento" @bind-Value="@TiposDocumentoSelecionados" ShowSelectAll="true" EnableSelectionOrder="false" ShowDropDownIcon="true" FilterBarPlaceholder="Pesquise..." PopupHeight="350px">
<MultiSelectFieldSettings Value="TipoDocumento" Text="Nome"></MultiSelectFieldSettings>
<MultiSelectEvents TItem="TipoDocumentoDistribuicaoVM" TValue="List<TipoDocumentoDistribuicaoVM>" ValueChange="@TiposDeDocumentosSelecionadosValueChangeHandler"></MultiSelectEvents>
</SfMultiSelect>
The bind-Value="@TiposDocumentoSelecionados" points to a variable which I exposed as parameter, like this
[Parameter]
public ListTiposDocumentoSelecionados { get; set; }
So, in pages where I need my custom control, I can pass in the list of items I want to be rendered as "checked", so in those pages I do the follow:
<MyCustomComponent @ref="cmpSelecaoDocumentoAssunto" TiposDocumentoSelecionadosChanged="@TipoSelecionadosNoComponente" AssuntosSelecionadosChanged="DocsSelecionadosNoComponente" TiposDocumentoSelecionados="objRegraDistribuicaoVM.TiposDeDocumento"></MyCustomComponent>
At TiposDocumentoSelecionados="objRegraDistribuicaoVM.TiposDeDocumento" I am passing the list os items which should be rendered as"checked", but, none of the items of the SfMultiSelect appears checked.
How can I make this work?
OBS: I tried to find some method of the SfMultiSelect which select items by code but I only find the
cmpSelecaoDocumentoAssunto.SelectAllAsync(true) which works, but i need a method to select specific items,not all of them.
Thanks for your answer, the example you provided works as expected, but my code not. So I spend all my day investigating it and I found the problem, which I believe it is a bug in the SfMultiSelect.
I edited and documented your example do demonstrate the issue, anyway let me describe it here.
The problem is:
To make it work, both the itens in the Datasource and in the @bind-Value should have the exact same VALUES! Which is very unlikely to happen.
In my case, the issue happens because my model have this Id property:
public class Countries
{
public int Id { get; set; } // Maybe a property to represent the PK saved in the database, anyway I dont use it in the SfMultiSelect
public string Name { get; set; } // The Text of the SfMultiSelect
public string Code { get; set; } // The Value of the SfMultiSelect
}
Probably the Id property will be greater than ZERO when these entities are read from the database.
In the component the Id property of the datasource list will be ZERO because it was generated from an Enum I have.
I hope it make clear the issue and I hope you guys agree that it seems to be a bug.
Please, let me know if you will treat it as bug.
Also, let me know if you have some suggestions on how to workaround
Ok. I understand the details you provided above but please, do not mix the issues. In fact, the details you provided above will only confuse future readers of this post. I suggest to delete it.
The example I provided attached exemplifies exactly what is the problem. The SfMultiSelect should consider ONLY the property used as Value in the MultiSelectFieldSettings to determine if an item should be rendered as checked or not.
In the example below, ONLY the property Code, should be considered.
<MultiSelectFieldSettings Text="Name" Value="Code"></MultiSelectFieldSettings>
Any other property (including the Id property) has nothing to do in this case.
I already did this to workaround this problem. But this absolutely does not make sense, the right way to compare is looking ONLY to the property used as Value="Code" in the MultiSelectFieldSettings
Furthermore, it is common in projects to use ViewModels, those view model, will never be 100% equal to the underlying complex type (Model).
I understand that the way C# compare objects, so, if I override the object Equals() in my ComplexType, the SfMultiSelect will work as the expected? I don't think so. Can you test it?
The problem I reported will be treated as bug or it will be working as is?