如何在选择MudRadio时显示不同的组件

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

How do you display different Components when selecting MudRadio's

问题

点击MudRadio按钮(在MudDialog中)时,我想要实现的功能是,在MudDialog中显示不同的组件。

我搜索了一个示例,但找不到我要找的内容。

英文:

I would like to implement the functionality when clicking a MudRadio button (in a MudDialog), it should display a different component in the the MudDialog.

I searched for an example and couldn't find what I'm looking for

答案1

得分: 1

以下是您要的中文翻译部分:

你需要有一个 MudRadioGroup 来容纳你的 MudRadio 按钮。在 MudRadioGroup 上,你有 @bind-SelectedOption 事件回调可供使用。你可以在那里设置一个开关语句,以动态渲染你的其他组件。

以下是一个示例项目,可以完成你想要做的事情:

https://try.mudblazor.com/snippet/wOmRucPSpMDbtnAw

ParentComponent.razor

<MudForm>
    <MudRadioGroup @bind-SelectedOption="SelectedOption">
        <MudRadio Option="@(Add)" Color="Color.Primary">Primary</MudRadio>
        <MudRadio Option="@(Radio 2)" Color="Color.Secondary">Secondary</MudRadio>
        <MudRadio Option="@(Radio 3)">Default</MudRadio>
        <MudRadio Option="@(Radio 4)" Color="Color.Primary" Disabled="true">Disabled</MudRadio>
    </MudRadioGroup>
</MudForm>

@switch(SelectedOption)
{
    case "Add":
        <TestComponent />
    break;
}

<div class="d-flex align-center">
    <MudButton Variant="Variant.Outlined" OnClick="Reset">Reset</MudButton>
    <MudText Class="ml-4">Selected Option: @SelectedOption</MudText>
</div>

@code {
    public string SelectedOption { get; set; }

    private void Reset()
    {
        SelectedOption = null;
    }
}

TestComponent.razor

<h1>来自 TestComponent 的问候</h1>
英文:

You need to have a MudRadioGroup to contain your MudRadio buttons. On the MudRadioGroup you have the @bind-SelectedOption event callback exposed. You can set a switch statement on that to dynamically render your other components.

Here is a sample project to accomplish what you're trying to do:

https://try.mudblazor.com/snippet/wOmRucPSpMDbtnAw

ParentComponent.razor

&lt;MudForm&gt;
    &lt;MudRadioGroup @bind-SelectedOption=&quot;@SelectedOption&quot;&gt;
        &lt;MudRadio Option=&quot;@(&quot;Add&quot;)&quot; Color=&quot;Color.Primary&quot;&gt;Primary&lt;/MudRadio&gt;
        &lt;MudRadio Option=&quot;@(&quot;Radio 2&quot;)&quot; Color=&quot;Color.Secondary&quot;&gt;Secondary&lt;/MudRadio&gt;
        &lt;MudRadio Option=&quot;@(&quot;Radio 3&quot;)&quot;&gt;Default&lt;/MudRadio&gt;
        &lt;MudRadio Option=&quot;@(&quot;Radio 4&quot;)&quot; Color=&quot;Color.Primary&quot; Disabled=&quot;true&quot;&gt;Disabled&lt;/MudRadio&gt;
    &lt;/MudRadioGroup&gt;
&lt;/MudForm&gt;

@switch(SelectedOption)
{
    case &quot;Add&quot;:
        &lt;TestComponent /&gt;
    break;
}

&lt;div class=&quot;d-flex align-center&quot;&gt;
    &lt;MudButton Variant=&quot;Variant.Outlined&quot; OnClick=&quot;Reset&quot;&gt;Reset&lt;/MudButton&gt;
    &lt;MudText Class=&quot;ml-4&quot;&gt;Selected Option: @SelectedOption&lt;/MudText&gt;
&lt;/div&gt;

@code {
    public string SelectedOption { get; set; }

    private void Reset()
    {
        SelectedOption = null;
    }
}

TestComponent.razor

&lt;h1&gt;Hi from: TestComponent&lt;/h1&gt;

huangapple
  • 本文由 发表于 2023年2月14日 22:33:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449319.html
匿名

发表评论

匿名网友

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

确定