tags:

views:

139

answers:

2

I am having a Grid in WPF application. I want to display all the text block on the first column in the grid to be aligned to the right. so I thought I could do it with ColumnDefinition and Style like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition>
            <ColumnDefinition.Resources>
                <Style TargetType={x:Type TextBlock}">
                    <Setter Property=....../>
                </Style..
.....

But This is is not working Any idea why?

+1  A: 

Is that what you want?

<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>
Marcel Benthin
A: 

You may have to try a PropertyTrigger, something like the following :

<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>
Alex Marshall