wpf 中 datagrid标题栏显示中文

xaml页面

<DataGrid Grid.Row="2" x:Name="date" AutoGeneratingColumn="Date_AutoGeneratingColumn"  AutoGenerateColumns="True" Margin="10">
                        <DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <Setter Property="Height" Value="40"/>
                                <Setter Property="FontSize" Value="30"></Setter>
                            </Style>
                        </DataGrid.RowStyle>
                    </DataGrid>

添加AutoGeneratingColumn事件

private void Date_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            var result = e.PropertyName;
            var p = (e.PropertyDescriptor as PropertyDescriptor).ComponentType.GetProperties().FirstOrDefault(x => x.Name == e.PropertyName);

            if (p != null)
            {
                var found = p.GetCustomAttribute<DisplayAttribute>();
                if (found != null) result = found.Name;
            }

            e.Column.Header = result;
        }

 在定义实体类上面加标记

[Display(Name = "产线")]
public string CellId { get; set; }

原文地址:https://www.cnblogs.com/v587yy/p/11775944.html