我有一个MudBlazor DataGrid,其中一些单元格有很长的文本字符串.默认情况下,MudBlazor会将列宽设置为最长单元格值的长度.我希望默认的列宽是一个特定的宽度,如100px,如果需要,用户可以调整列的大小,以查看额外的文本.我也不希望文本换行.这就是它在Excel中的工作方式.我正在努力想办法做到这一点.

我试过使用CellStyle和HeaderStyle,但运气不是很好.我制作了一个基本的DataGrid来显示这个问题.下面是我的代码:

<div style="width: 50vw;">
  <MudDataGrid T="Person" Items="@People" ColumnResizeMode="ResizeMode.Container">
    <Columns>
      <PropertyColumn Property="x => x.Name" CellStyle="width: 100px; overflow-x: hidden; white-space: nowrap;" />
      <PropertyColumn Property="x => x.State" CellStyle="width: 100px; overflow-x: hidden; white-space: nowrap;" />
      <PropertyColumn Property="x => x.Age" CellStyle="width: 100px; overflow-x: hidden; white-space: nowrap;" />
    </Columns>
  </MudDataGrid>
</div>

@code {

  private IEnumerable<Person> People = new List<Person>
  {
    new Person("Brady", 25, "Florida"),
    new Person("Tim", 32, "Minnesota"),
    new Person("Derek", 51, "Utahqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"),
    new Person("Ben", 27, "Wisconsin"),
    new Person("Sam", 34, "Alaska")
  };

}

Here is what the output looks like for the above code:

在Excel中,列可以具有设置的宽度.如果单元格中的文本长度超过列宽,它就会被截断.用户需要调整列宽才能看到附加文本.这就是我希望在我的DataGrid中做的事情.

Here is what it looks like in Excel and what I'm trying to do.

推荐答案

您需要使用max-width个css属性,而不是width.

<div style="width: 50vw;">
  <MudDataGrid T="Person" Items="@People" ColumnResizeMode="ResizeMode.Container">
    <Columns>
      <PropertyColumn Property="x => x.Name" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap;" />
      <PropertyColumn Property="x => x.State" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap;" />
      <PropertyColumn Property="x => x.Age" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap;" />
    </Columns>
  </MudDataGrid>
</div>

@code {

  private IEnumerable<Person> People = new List<Person>
  {
    new Person("Brady", 25, "Florida"),
    new Person("Tim", 32, "Minnesota"),
    new Person("Derek", 51, "Utahqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"),
    new Person("Ben", 27, "Wisconsin"),
    new Person("Sam", 34, "Alaska")
  };
    public record Person(string Name, int Age, string State);
}
Generates

Example

MudBlazor snippet

Csharp相关问答推荐

使用GeneratedComInterfaceProperty的.NET 8 COM类对于VB 6/SYS或ALEViewer不可见

MongoDB实体框架核心:表达必须可写

. NET WireMock拒绝PostAsJsonAsync序列化

我需要两个属性类吗

如何使用XmlSerializer反序列化字符串数组?

实现List T,为什么LINQ之后它不会返回MyList?<>(无法强制转换WhereListIterator `1类型的对象)'

WeakReference未被垃圾收集

最新的Mediatr和具有同步方法的处理程序Handle:并非所有代码路径都返回值"

静态对象构造顺序

我如何让我的秒表保持运行场景而不重置

.NET并发词典交换值

有没有类似于扩展元素的合并元组的语法?

正在try 从Blazor中的API读取JSON

有没有更好的方法来在CosmosDB上插入非id?

如何允许数组接受多个类型?

获取混淆&Quot;模糊引用&Quot;错误

此异步方法在重写方法中缺少等待运算符警告

如何在C#中正确类型化带有泛型的嵌套类

为什么连接到Google OAuth2后,结果.Credential为空?

为什么Visual Studio 2022建议IDE0251将我的方法设置为只读?