0

I'm trying to create a new row in my listview.

This row will be always the last element, and is the sum of the fields in the upper rows.

For example:

<ListView ItemsSource={Binding}> 
<ListView.View> 
  <GridView> 
      <GridViewColumn Header="Column1" DisplayMemberBinding="{Binding Path=VarX}"/>
      <GridViewColumn Header="Column2" DisplayMemberBinding="{Binding Path=VarY}"/>
  </GridView>
</ListView.View>
</ListView> 

Now, what i want is display a new ROW that, in the first column displays a string like "The sum is: " and in the second column, sum the values of VarX and VarY.

Thanks and sorry if it is not well explained

Dan Delay
  • 102
  • 2
  • 12
  • I'm sorry but your question doesn't make any sense. What is the last line supposed to be? If you need to sum rows then you will have to write code to do so. – GazTheDestroyer Feb 10 '12 at 13:29
  • yep, I edited my question to make it clearer. Thanks! – Dan Delay Feb 10 '12 at 13:58
  • 2
    Perhaps [this answer about adding a footer to GridViews](http://stackoverflow.com/a/679996/302677) will help you – Rachel Feb 10 '12 at 14:04

1 Answers1

0

There are a couple of ways you can achieve this and while neither of these are good ways of doing it technically, they should keep you moving. I'm going to assume you are familiar with an MVVM paradigm (if not just search "MVVM WPF").

Without a grid View

If you are happy to display your data in columns without necessarily using a gridview. For example you can simply use a list box and set your items source to an IEnumerable and supply a list of differnt typed values. The final can be your summary row. Just supply a for each type and you end up with different row formats. i.e.

return ((IEnumerable<object>)model.Pairs)
    .Concat(new SummaryRow(model.Pairs)); 

And the XAML

<ListBox Grid.IsSharedSizeScope ItemsSource={Binding}>
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type IntPair}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="XColumn"/>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="YColumn"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding VarX}">
                <TextBlock Grid.Column="1" Text="{Binding VarY}">
            </Grid>
        </DataTemplate>
        <DataTemplate DataType="{x:Type SummaryRow}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="XColumn"/>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="YColumn"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="The sum is:">
                <TextBlock Grid.Column="1" Text="{Binding Total}">
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

With a GridView

Alternatively just use a ViewModel to supply an IEnumerable. So your model might have an IEnumerable, and you could do something as simple as

return (from x in model.Pairs
    select new ConvertedResults 
    { 
        VarX = x.IntX.ToString(), 
        VarY = x.IntY.ToString() 
    })
    .Concat(new [] { new ConvertedResults 
    { 
        VarX = "The sum is:",
        VarY = (from y in model.Pairs select x.VarX).Sum().ToString()
    });
AlSki
  • 6,868
  • 1
  • 26
  • 39