2
\$\begingroup\$

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>
\$\endgroup\$
4
  • \$\begingroup\$ strange.. the markup was invisible :D \$\endgroup\$
    – Revious
    Commented May 30, 2014 at 13:44
  • 1
    \$\begingroup\$ you must not have had it indented correctly \$\endgroup\$
    – Malachi
    Commented May 30, 2014 at 13:46
  • 2
    \$\begingroup\$ What do you mean by "syntetic syntax"? \$\endgroup\$ Commented May 30, 2014 at 13:51
  • \$\begingroup\$ @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 \$\endgroup\$
    – Revious
    Commented May 30, 2014 at 13:57

1 Answer 1

5
\$\begingroup\$

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.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented May 30, 2014 at 14:06
  • \$\begingroup\$ @GeorgeHowarth: yes, I guess I'm looking for something similar.. \$\endgroup\$
    – Revious
    Commented May 30, 2014 at 14:08
  • \$\begingroup\$ 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. \$\endgroup\$
    – Revious
    Commented May 30, 2014 at 14:09
  • 1
    \$\begingroup\$ @Revious If you're looking to define the CellTemplate as a resource here's how you do it. \$\endgroup\$ Commented May 30, 2014 at 14:10
  • 1
    \$\begingroup\$ @Revious the markup you have posted doesn't show 20 times the same identical structure... \$\endgroup\$ Commented May 30, 2014 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.