I am trying to center my Columns in my DataGrid by using:
<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>
I keep getting the error that is labeled in the title whenever using this XAML. I have tried creating this as a Style and using a Namespace, but that returned the same error as well. Any ideas?
Try this:
<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>
You are missing the <DataGridTextColumn>
tag
<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>
This is what worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.