c#WPF - 在DataGrid中添加总行

时间:2014-02-21 10:19:55

标签: c# .net wpf datagrid caliburn.micro

我有这个DataGrid:

enter image description here

我想在DataGrid的底部添加一个包含名称的行:总计和价格:所有价格的总和。如果可能该行将被冻结。 你有什么想法? 简单的项目在此链接中:here

编辑1 这是XAML代码:

<DataGrid ItemsSource="{Binding Articles}" AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Price" Binding="{Binding Price}" />               
        </DataGrid.Columns>
    </DataGrid>

这是ViewModel:

[Export(typeof(IShell))]
public class MainViewModel : Screen
{
    public System.ComponentModel.ICollectionView Articles { get; set; }

    public MainViewModel()
    {
        Articles = CollectionViewSource.GetDefaultView(GetArticles());
    }


    private List<Article> GetArticles()
    {
        List<Article> arts = new List<Article>();

        arts.Add(new Article { Name = "Name1", Price = 2.80 });
        arts.Add(new Article { Name = "Name2", Price = 1.25 });
        arts.Add(new Article { Name = "Name3", Price = 9.32 });
        arts.Add(new Article { Name = "Name4", Price = 1.31 });
        arts.Add(new Article { Name = "Name5", Price = 0.80});
        arts.Add(new Article { Name = "Name6", Price = 2.50});
        arts.Add(new Article { Name = "Name7", Price = 0.50 });

        return arts;
    }

}

3 个答案:

答案 0 :(得分:2)

您可以在数据网格下方添加一个文本块,并将其绑定到具有总价格的属性,而不是将最后一行添加到数据网格中。

答案 1 :(得分:1)

这应该是有用的。我只添加了一个新行,其中包含了一个总和

的方法
private List<Article> GetArticles()
    {
        List<Article> arts = new List<Article>();

        arts.Add(new Article { Name = "Name1", Price = 2.80 });
        arts.Add(new Article { Name = "Name2", Price = 1.25 });
        arts.Add(new Article { Name = "Name3", Price = 9.32 });
        arts.Add(new Article { Name = "Name4", Price = 1.31 });
        arts.Add(new Article { Name = "Name5", Price = 0.80 });
        arts.Add(new Article { Name = "Name6", Price = 2.50 });
        arts.Add(new Article { Name = "Name7", Price = 0.50 });
        arts.Add(new Article {Name = "Total", Price = GetTotal(arts)});
        return arts;
    }

    private double GetTotal(List<Article> arts)
    {
        double Total = 0;
        foreach (Article art in arts)
        {
            Total += double.Parse(art.Price.ToString());
        }
        return Total;
    }

此致

答案 2 :(得分:1)

再来一次:

private List<Article> GetArticles()
    {
        List<Article> arts = new List<Article>();

        arts.Add(new Article { Name = "Name1", Price = 2.80 });
        arts.Add(new Article { Name = "Name2", Price = 1.25 });
        arts.Add(new Article { Name = "Name3", Price = 9.32 });
        arts.Add(new Article { Name = "Name4", Price = 1.31 });
        arts.Add(new Article { Name = "Name5", Price = 0.80 });
        arts.Add(new Article { Name = "Name6", Price = 2.50 });
        arts.Add(new Article { Name = "Name7", Price = 0.50 });
        arts.Add(new Article { Name = "Total", Price = arts.Sum(l => l.Price)});
        return arts;
    }