从实体数据模型向WPF DataGrid默认视图添加计算列

时间:2017-08-07 12:54:36

标签: c# .net wpf entity-framework xaml

我是WPF和实体框架的n00b。我从数据库导入了代码优先模型,并将生成的上下文拖到DataGrid XAML元素中。我想在默认视图中添加一个额外的列,从简单的计算中派生出来。

目前显示

date  | revenue | cost

12/01 | 200     | 100

12/20 | 300     | 90

如果不修改MS SQL数据库,如何在视图中添加一列,以便datagrid显示收入成本的“利润”列?

date  | revenue | cost | profit

12/01 | 200     | 100  | 100

12/20 | 300     | 90   | 210

这是我的MainWindow.xaml:

<Window x:Class="VenmeWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:VenmeWPF"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
    <CollectionViewSource x:Key="venmeContextViewSource" d:DesignSource="{d:DesignInstance {x:Type local:VenmeContext}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource venmeContextViewSource}">
    <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="56,79,0,0" VerticalAlignment="Top" Height="141" Width="424" ItemsSource="{Binding}"/>

</Grid>

这是我的MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    private VenmeContext _venmeContext = new VenmeContext();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        System.Windows.Data.CollectionViewSource venmeContextViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("venmeContextViewSource")));
        // Load data by setting the CollectionViewSource.Source property:
        _venmeContext.Transactions.Load();
        venmeContextViewSource.Source = _venmeContext.Transactions.Local;
    }
}

来自ADO.NET实体数据模型的VenmeContext.cs:

public partial class VenmeContext : DbContext
{
    public VenmeContext()
        : base("name=VenmeContext")
    {
    }

    public virtual DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }
    public virtual DbSet<Transaction> Transactions { get; set; }
    public virtual DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .Property(e => e.name)
            .IsUnicode(false);

        modelBuilder.Entity<User>()
            .HasMany(e => e.Transactions)
            .WithRequired(e => e.User)
            .HasForeignKey(e => e.FromUserId);
    }
}

Transactions.cs也来自ADO.NET实体数据模型:

public partial class Transaction
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int id { get; set; }

    public DateTime date { get; set; }

    public int revenue { get; set; }

    public int cost { get; set; }
}

1 个答案:

答案 0 :(得分:1)

在XAML标记中添加一列:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="56,79,0,0" 
          VerticalAlignment="Top" Height="141" Width="424" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding profit}" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>

...并创建另一个部分类,您可以在其中定义附加属性:

public partial class Transaction
{
    public int profit
    {
        get
        {
            return revenue - cost;
        }
    }
}

请注意,必须在与自动生成的部分类相同的命名空间和程序集(项目)中定义部分类:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods