在DataGridTextColumn

时间:2016-10-03 16:29:22

标签: c# wpf

我试图通过使用:

将我的列集中在我的DataGrid中
<DataGrid x:Name="dgvMain" Margin="10,438,10,10" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" IsReadOnly="True" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGrid.Columns>
    </DataGrid>

每次使用此XAML时,我都会一直收到标题中标注的错误。我尝试将其创建为样式并使用命名空间,但也返回了相同的错误。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

<DataGrid x:Name="dgvMain" Margin="10,438,10,10" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" IsReadOnly="True" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn>
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        <DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

您缺少<DataGridTextColumn>代码

答案 1 :(得分:0)

<DataGrid x:Name="dgvMain" Margin="10,438,10,10" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" IsReadOnly="True" ItemsSource="{Binding}">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>

这对我有用。