I have a DataGridTextColumn, and wanted to set the text in the middle, I did this XAML :
<DataGridTextColumn Header="Smooth" Binding="{Binding Smoothing}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBox">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
That worked fine. I have a lot of DataGridTextColumn, so I wanted to extract the style as a resource. Which looks like :
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="TextBoxStyle">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</UserControl.Resources>
and I do this for my GridItem :
<DataGridTextColumn Header="Smooth" Binding="{Binding Smoothing}" ElementStyle="{StaticResource ResourceKey=TextBoxStyle}"/>
I actually get a crash complaining when checking the TargetType. Any ideas why the first parts work but not the second part?
Thanks
Your
DataGridTextColumn.ElementStyleshould be targetted to aTextBlockand NOTTextBox.Your
DataGridTextColumn.EditingElementStyleshould be the one that targets aTextBox(if your data grid or column is editable)(Simply because readonly text cell has TextBlock and text cell in edit mode has a TextBox in it)