MudBlazor: 在多选中显示自定义文本

huangapple go评论126阅读模式
英文:

MudBlazor: Display customized text in Muti-select

问题

我正在尝试在MudBlazor的多选(Multi-Select)中显示自定义文本。
一切都正常,但显示的文本是"IndicatorDefinition"而不是自定义文本。

代码快速浏览:

  1. <MudSelect Margin="Margin.Dense" T="IndicatorDefinition" MultiSelection="true"
  2. SelectAll="true" SelectAllText="Select All"
  3. @bind-SelectedValues="_selectedIndicators"
  4. Style="width: 100px;" Clearable="true">
  5. @foreach (var indicatorDefinition in IndicatorDefinitions)
  6. {
  7. <MudSelectItem Value="@indicatorDefinition">@indicatorDefinition.SubTitle</MudSelectItem>
  8. }
  9. </MudSelect>
  10. @code {
  11. public string Text {
  12. get{
  13. return $"{_selectedIndicators.Count()}/{IndicatorDefinitions.Count()} selected.";
  14. }
  15. set { var _ = value;}
  16. }
  17. public List<IndicatorDefinition> IndicatorDefinitions { get; set; } = new()
  18. {
  19. new () {SubTitle= "First"},
  20. new () {SubTitle= "Second"}
  21. };
  22. public IEnumerable<IndicatorDefinition> _selectedIndicators = new List<IndicatorDefinition>();
  23. public class IndicatorDefinition
  24. {
  25. public string SubTitle { get; set; }
  26. public double Value { get; set; }
  27. }
  28. }

沙盒示例:https://try.mudblazor.com/snippet/wYQHaiuJGgaBWuxA

选择(Select)的参考文档:https://www.mudblazor.com/components/select#variants

我认为问题在于T的值为"IndicatorDefinition"而不是字符串(string)。我考虑了一种方法,即:

  1. 使用T = "string"。
  2. 将选定的值保存在List中。
  3. 从List重新构建_selectedItems。

但这并不是一种优雅的解决方案。

谢谢大家。

英文:

I'm trying to display customized text in multi-select of MudBlazor.
Everything is fine but the text displayed is "IndicatorDefinition" rather than the customized text.

A quick look of code:

  1. &lt;MudSelect Margin=&quot;Margin.Dense&quot; T=&quot;IndicatorDefinition&quot; MultiSelection=&quot;true&quot;
  2. SelectAll=&quot;true&quot; SelectAllText=&quot;Select All&quot;
  3. @bind-SelectedValues=&quot;_selectedIndicators&quot;
  4. Style=&quot;width: 100px;&quot; Clearable=&quot;true&quot;&gt;
  5. @foreach (var indicatorDefinition in IndicatorDefinitions)
  6. {
  7. &lt;MudSelectItem Value=&quot;@indicatorDefinition&quot;&gt;@indicatorDefinition.SubTitle&lt;/MudSelectItem&gt;
  8. }
  9. &lt;/MudSelect&gt;
  10. @code {
  11. public string Text {
  12. get{
  13. return $&quot;{_selectedIndicators.Count()}/{IndicatorDefinitions.Count()} selected.&quot;;
  14. }
  15. set { var _ = value;}
  16. }
  17. public List&lt;IndicatorDefinition&gt; IndicatorDefinitions { get; set; } = new()
  18. {
  19. new () {SubTitle= &quot;First&quot;},
  20. new () {SubTitle= &quot;Second&quot;}
  21. };
  22. public IEnumerable&lt;IndicatorDefinition&gt; _selectedIndicators = new List&lt;IndicatorDefinition&gt;();
  23. public class IndicatorDefinition
  24. {
  25. public string SubTitle { get; set; }
  26. public double Value { get; set; }
  27. }
  28. }

Sandbox here: https://try.mudblazor.com/snippet/wYQHaiuJGgaBWuxA

Reference of select here: https://www.mudblazor.com/components/select#variants

I think it's because the T is "IndicatorDefinition" rather than string. I thought of a way, which is:

  1. Use T = "string" instead
  2. Save selected value in List<string>
  3. Rebuild _selectedItems from List<string>.

But it's not elegant.

Thanks all.

答案1

得分: 1

你可以使用 ToStringFunc 函数委托。定义了如何在下拉列表中显示值,来源于 文档

  1. <MudSelect Margin="Margin.Dense" T="IndicatorDefinition" MultiSelection="true"
  2. SelectAll="true" SelectAllText="Select All"
  3. @bind-SelectedValues="_selectedIndicators"
  4. Style="width: 100px;" Clearable="true"
  5. ToStringFunc="@(e => e.SubTitle)">
  6. @foreach (var indicatorDefinition in IndicatorDefinitions)
  7. {
  8. <MudSelectItem Value="@indicatorDefinition">@indicatorDefinition.SubTitle</MudSelectItem>
  9. }
  10. </MudSelect>
英文:

You can use the ToStringFunc func delegate.

Defines how values are displayed in the drop-down list , from docs

  1. &lt;MudSelect Margin=&quot;Margin.Dense&quot; T=&quot;IndicatorDefinition&quot; MultiSelection=&quot;true&quot;
  2. SelectAll=&quot;true&quot; SelectAllText=&quot;Select All&quot;
  3. @bind-SelectedValues=&quot;_selectedIndicators&quot;
  4. Style=&quot;width: 100px;&quot; Clearable=&quot;true&quot;
  5. ToStringFunc=&quot;@(e =&gt; e.SubTitle)&quot;&gt;
  6. @foreach (var indicatorDefinition in IndicatorDefinitions)
  7. {
  8. &lt;MudSelectItem Value=&quot;@indicatorDefinition&quot;&gt;@indicatorDefinition.SubTitle&lt;/MudSelectItem&gt;
  9. }
  10. &lt;/MudSelect&gt;

huangapple
  • 本文由 发表于 2023年8月5日 15:23:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定