# Rewriting WPF GridViewColumn alignment in a less verbose way

I wanted just to right align one column in WPF and I found out that the syntax is not exactly syntetic... Is there a way to make it more syntetic?

Consider that this one colum, but I have 20 columns which share the same identical structure, changing only the displayed property.

<GridViewColumn Header="trial" Width="110">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:N} {1}">
<Binding Path="Income"></Binding>
<Binding ElementName="UserControl" Path="DataContext.Pinco"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

• strange.. the markup was invisible :D – Revious May 30 '14 at 13:44
• you must not have had it indented correctly – Malachi May 30 '14 at 13:46
• What do you mean by "syntetic syntax"? – Simon Forsberg May 30 '14 at 13:51
• @SimonAndréForsberg: for saying: TextAlign="right", StringFormat="{}{0:N} {1} + DataContext.Pinco" I need 14 lines... it's not very readable. In CSS I can do the same in a more synthetic way. And the existance of classes is to avoid code repetitions. Here I have to duplicate the code for every column!!! :O – Revious May 30 '14 at 13:57

XAML is XAML - I'm not sure what the meaning of "syntetic" is in this context, but the only thing I can see that could improve, is the closing tags for <Binding> - the empty elements can be collapsed:

<MultiBinding StringFormat="{}{0:N} {1}">
<Binding Path="Income" />
<Binding ElementName="UserControl" Path="DataContext.Pinco" />
</MultiBinding>


There aren't 20 different ways of specifying the horizontal alignment of a TextBlock:

<TextBlock HorizontalAlignment="Right">


I don't see anything wrong with this.

Actually the indentation could use more spaces, too - but that's cosmetic (and probably just a copy/paste glitch with the tabs):

<GridViewColumn Header="trial" Width="110">
<GridViewColumn.CellTemplate>
<DataTemplate>

<TextBlock HorizontalAlignment="Right">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:N} {1}">
<Binding Path="Income" />
<Binding ElementName="UserControl" Path="DataContext.Pinco" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>


I like using vertical whitespace to separate things - it makes the markup easier to read, and maintain.

EDIT: Considering that there would be 20x such markup, with the only thing changing being the <Binding Path="Income" /> part, as @GeorgeHowarth commented you can look into making the DataTemplate a StaticResource, like this.

• To eliminate all verbosity I guess you could define the DataTemplate as a XAML resource. Maybe it's worth adding in the answer as an alternative option. – George Howarth May 30 '14 at 14:06
• @GeorgeHowarth: yes, I guess I'm looking for something similar.. – Revious May 30 '14 at 14:08
• You know when people refactor some similar c# method? the DRY principle? Avoid code duplication.. this is what I mean... an alternative to avoid this verbosity for defining 20 times the same identical structure of table. – Revious May 30 '14 at 14:09
• @Revious If you're looking to define the CellTemplate as a resource here's how you do it. – George Howarth May 30 '14 at 14:10
• @Revious the markup you have posted doesn't show 20 times the same identical structure... – Mathieu Guindon May 30 '14 at 14:12