Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put XAML right-align code into a style?

Tags:

xaml

It taking 5 lines to right-align a DataGridTextColumn is bloating my XAML.

How can I put this "right-align code block" into a style and call it from an attribute, e.g. class="rightAlignDataGridTextColumn".

<toolkit:DataGridTextColumn Header="Order Date" Width="Auto"
       Binding="{Binding OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}" 
       IsReadOnly="False">
    <toolkit:DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Right" />
        </Style>
    </toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>

<toolkit:DataGridTextColumn Header="Required Date" Width="Auto"
       Binding="{Binding RequiredDate, StringFormat='{}{0:dd.MM.yyyy}'}" 
       IsReadOnly="False">
    <toolkit:DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Right" />
        </Style>
    </toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>

<toolkit:DataGridTextColumn Header="Shipped Date" Width="Auto"
       Binding="{Binding ShippedDate, StringFormat='{}{0:dd.MM.yyyy}'}" 
       IsReadOnly="False">
    <toolkit:DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Right" />
        </Style>
    </toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>
like image 721
Edward Tanguay Avatar asked Mar 26 '09 14:03

Edward Tanguay


1 Answers

Declare the Style in a ResourceDictionary, like so:

<Window.Resources>
    <Style x:Key="RightAlignStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="TextAlignment" Value="Right" />
    </Style>
</Window.Resources>

Then reference this style on each DataGridTextColumn element:

<toolkit:DataGridTextColumn ElementStyle="{StaticResource RightAlignStyle}" ... >
like image 81
Bradley Grainger Avatar answered Dec 18 '22 08:12

Bradley Grainger