如何在MudBlazor堆栈中定义比例?

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

How to define proportions in MudBlazor stack?

问题

我复制了一个包含3个纸张元素的MudBlazor堆栈示例,并更改它们的大小比例,使最后一个更长:

<MudStack Row="true">
    <MudPaper Class="px-1">Item 1</MudPaper>
    <MudPaper Class="px-1">Item 2</MudPaper>
    <MudPaper Class="px-10">Item 3</MudPaper>
</MudStack>

但我在这方面有两个问题:

  • 即使对于这个简单的代码,最后一个项目也不是其他项目的10倍长,虽然它更长,但不多,
  • 当我尝试将其他组件如MudSelectMudTextField用作堆栈元素时,它不起作用。大小在元素之间均匀分布

那么如何一般表达"使第K个元素比第L个元素宽/高N倍"的概念呢?

假设可以使用堆栈来实现

英文:

I copied a stack example with 3 paper elements from MudBlazor and change their size proportions so the last one is longer:

&lt;MudStack Row=&quot;true&quot;&gt;
    &lt;MudPaper Class=&quot;px-1&quot;&gt;Item 1&lt;/MudPaper&gt;
    &lt;MudPaper Class=&quot;px-1&quot;&gt;Item 2&lt;/MudPaper&gt;
    &lt;MudPaper Class=&quot;px-10&quot;&gt;Item 3&lt;/MudPaper&gt;
&lt;/MudStack&gt;

But I have two problems with this:

  • even for this simple code the last item is not 10 times longer than any of the other items, it is longer, but not much,
  • when I try to use other components like MudSelect or MudTextField as stack elements it does not work. The size is evenly distributed between elements

So how to express in general the notion "make Kth element N times wider/higher than Lth one"?

Assuming it is doable using stack.

答案1

得分: 1

px-1CSS实用类,其中p表示填充(padding),x表示x轴。填充不能用于实现您所需的行为,因为它仅与内容和其边框相关。

要设置组件的宽度和高度,您需要通过使用组件的元素的width/height属性来更改,可以使用组件的Style属性,或者对于一些组件,例如MudPaperWidth/Height被公开为参数供您使用。虽然您仍然可以简单地为这些组件使用Style属性。

<MudStack Row="true">
    <MudPaper Class="px-1" Width="@($"{_width}px")">Item 1</MudPaper>
    <MudPaper Class="px-1" Width="@($"{_width*2}px")">Item 2</MudPaper>
    <MudPaper Class="px-1" Width="@($"{_width*10}px")">Item 3</MudPaper>
</MudStack>

<MudStack Row="false">
    <MudTextField Style="@($"width:{_width}px")" @bind-Value="TextValue" Label="1x" Variant="Variant.Text"></MudTextField>
    <MudTextField Style="@($"width:{_width*2}px")" @bind-Value="TextValue" Label="2x" Variant="Variant.Text"></MudTextField>
    <MudTextField Style="@($"width:{_width*3}px")" @bind-Value="TextValue" Label="3x" Variant="Variant.Text"></MudTextField>
</MudStack>
@code{
    int _width = 200;
    public string TextValue { get; set; }
}

MudBlazor代码片段

英文:

px-1 is CSS Utility class where p is for padding and x is for the x-axis. And padding can't be used to achieve your desired behavior as it is only relative to the content and it's borders.

To set the width and height of components you will need to change the width/height attributes of the component's elements by either using the Style property of the component or for some components like MudPaper, the Width/Height are exposed as parameters for you to use. Although you can still also simply use the Style property for these aswell.

&lt;MudStack Row=&quot;true&quot;&gt;
    &lt;MudPaper Class=&quot;px-1&quot; Width=&quot;@($&quot;{_width}px&quot;)&quot;&gt;Item 1&lt;/MudPaper&gt;
    &lt;MudPaper Class=&quot;px-1&quot; Width=&quot;@($&quot;{_width*2}px&quot;)&quot;&gt;Item 2&lt;/MudPaper&gt;
    &lt;MudPaper Class=&quot;px-1&quot; Width=&quot;@($&quot;{_width*10}px&quot;)&quot;&gt;Item 3&lt;/MudPaper&gt;
&lt;/MudStack&gt;

&lt;MudStack Row=&quot;false&quot;&gt;
    &lt;MudTextField Style=&quot;@($&quot;width:{_width}px&quot;)&quot; @bind-Value=&quot;TextValue&quot; Label=&quot;1x&quot; Variant=&quot;Variant.Text&quot;&gt;&lt;/MudTextField&gt;
    &lt;MudTextField Style=&quot;@($&quot;width:{_width*2}px&quot;)&quot; @bind-Value=&quot;TextValue&quot; Label=&quot;2x&quot; Variant=&quot;Variant.Text&quot;&gt;&lt;/MudTextField&gt;
    &lt;MudTextField Style=&quot;@($&quot;width:{_width*3}px&quot;)&quot; @bind-Value=&quot;TextValue&quot; Label=&quot;3x&quot; Variant=&quot;Variant.Text&quot;&gt;&lt;/MudTextField&gt;
&lt;/MudStack&gt;
@code{
    int _width = 200;
    public string TextValue { get; set; }
}

MudBlazor snippet

huangapple
  • 本文由 发表于 2023年7月18日 03:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707423.html
匿名

发表评论

匿名网友

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

确定