我的问题类似于Why DataColumn.Caption doesn't work?,但对于WPF。我使用MVVM模式将DataGrid绑定(bind)到DataTableDataGrid具有AutoGenerateColumns = true。如何将DataGridColumn header 文本绑定(bind)到DataColumn.Caption而不是DataColumn.ColumnName?我一直希望这样的解决方案:

<DataGrid ItemsSource="MyDataTable" AutoGenerateColumns="true">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{Binding DataColumn.Caption}"> <!--this does not work-->
...
</DataGrid>

最佳答案

我最终在后面的代码中解决了这个问题。

private void dgResults_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    e.Column.Header = tableResults.Columns[e.PropertyName].Caption;
}

关于wpf - 将DataColumn.Caption绑定(bind)到DataGrid header ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41453681/

10-14 11:10
查看更多