我在WPF应用程序中有一个Grid。 我想在网格的第一列显示所有文本块以与右对齐。 所以我认为我可以用ColumnDefinition和Style这样做:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition>
<ColumnDefinition.Resources>
<Style TargetType={x:Type TextBlock}">
<Setter Property=....../>
</Style..
.....
但这不起作用 知道为什么吗?
答案 0 :(得分:1)
这就是你想要的吗?
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Grid.Column" Value="0" />
<Setter Property="TextAlignment" Value="Right" />
</Style>
</Grid.Resources>
<TextBlock Text="foo" Grid.Row="0"/>
<TextBlock Text="bar" Grid.Row="1" />
</Grid>
答案 1 :(得分:1)
您可能需要尝试使用PropertyTrigger,如下所示:
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Grid.Column" Value="0">
<Setter Property="TextAlignment" Value="Right"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>