I have a WPF Grid with many rows and columns, all containing things like TextBlocks and TextBoxes.
For this specific situation I want all the stuff in column 1 to ha
You can define some styles like below and assign them to your Column.ElementStyle property:
<Window.Resources>
<Style x:Key="elementStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="2,0,2,0" />
</Style>
<Style x:Key="rightElementStyle" BasedOn="{StaticResource elementStyle}" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
<Style x:Key="centerElementStyle" BasedOn="{StaticResource elementStyle}" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Window.Resources>
<dg:DataGrid AutoGenerateColumns="False">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding={Binding Path=Name}
Header="Name"
ElementStyle="{StaticResource centerElementStyle}"/>
<dg:DataGridTextColumn Binding={Binding Path=Amount}
Header="Amount"
ElementStyle="{StaticResource rightElementStyle}"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
Here's what I usually do:
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Style.Triggers>
<Trigger Property="Grid.Column" Value="0">
<Setter Property="Margin" Value="0,0,2,0" />
</Trigger>
<Trigger Property="Grid.Column" Value="2">
<Setter Property="Margin" Value="20,0,2,0" />
</Trigger>
</Style.Triggers>
</Style>