【问题标题】:How to make DataGrid columns horizontalalignment stretch & customize column name - UWP DataGrid如何使 DataGrid 列水平对齐拉伸和自定义列名称 - UWP DataGrid
【发布时间】:2019-10-16 08:20:14
【问题描述】:

UWP 数据网格:DataGrid

Q1:我想让列的宽度平分,占据所有可用的宽度,怎么办?

xaml:

<controls:DataGrid x:Name="dt_Home" AutoGenerateColumns="True"
                           CanUserResizeColumns="False"
                           AlternatingRowBackground="Gainsboro"
                           ItemsSource="{Binding UserSource, Mode=OneWay}"
                           IsReadOnly="True"/>

我的 DataGrid ItemSource 在我的 ViewModel 中:

public ObservableCollection<User> UserSource { get; set; } = new ObservableCollection<User>();

Q2:我想修改列名,例如将FirstName列改为显示FIRST NAME,请问是怎么回事?

【问题讨论】:

  • 你尝试了什么?您能否也提供一个代码示例?
  • 请分享您的 XAML

标签: c# uwp


【解决方案1】:

Q1:我想让列的宽度平分,占据所有可用的宽度,怎么做?

如果您想让所有列占据所有可用的列,您可以按比例设置列宽。请将每个宽度设置为Width="*"

<controls:DataGrid.Columns>
    <controls:DataGridTextColumn
        Width="*"
        Binding="{Binding Id}"
        Header="ID"
        Tag="Id"
        />
    <controls:DataGridTextColumn
        Width="*"
        Binding="{Binding Title}"
        Header="Title"
        Tag="Title"
        />
    <controls:DataGridComboBoxColumn
        Width="*"
        Binding="{Binding Link}"
        Header="Link"
        ItemsSource="{x:Bind source}"
        Tag="Link"
        />
    <controls:DataGridTextColumn
        Width="*"
        Binding="{Binding Type}"
        Header="Type"
        Tag="Type"
        />
    <controls:DataGridTextColumn
        Width="*"
        Binding="{Binding Remark}"
        Header="Remark"
        Tag="Remark"
        />
    <controls:DataGridTextColumn
        Width="*"
        Binding="{Binding Time}"
        Header="Time"
        Tag="Time"
        />
</controls:DataGrid.Columns>

Q2:我想修改列名,比如把FirstName列改成显示FIRST NAME,是怎么回事?

FirstName 将显示 DataGridTextColumn Header 属性,因此您可以将 FirstName 编辑为 FIRST NAME,如下所示。

<controls:DataGridTextColumn
    Width="*"
    Binding="{Binding FName}"
    Header="FIRST NAME"
    Tag="Time"
    />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2021-12-19
    • 2011-01-26
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 2012-11-07
    相关资源
    最近更新 更多