WPF网格 - 如何仅为一列应用样式?

Jac*_*ley 10 wpf grid

我有一个包含许多行和列的WPF网格,它们都包含TextBlocks和TextBoxes之类的东西.

对于这种特定情况,我希望第1列中的所有内容都有填充,第2列中的所有内容都要对齐.看起来非常非WPF,必须在网格中的每个项目上设置这些属性.

我知道我可以通过这样的方式为网格中的所有TextBlock创建一个样式:

<Grid>
  <Grid.Resources>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="HorizontalAlignment" Value="Right"/>
    </Style>
  </Grid.Resources>
</Grid>
Run Code Online (Sandbox Code Playgroud)

但有没有办法将该样式仅应用于第2列中的控件?

我应该使用不同的控件吗?

Bry*_*son 19

这是我通常做的事情:

<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>
Run Code Online (Sandbox Code Playgroud)