显示计算属性的实体框架和WPF界面 [英] Show Computed Property in Entity Framework and WPF UI

查看:354
本文介绍了显示计算属性的实体框架和WPF界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的EF数据模型下载实体。它的属性,规模和BytesDownloaded的二,计算给我,我已经在部分类创建的进度属性:

I have a Download Entity in my EF Data Model. Two of its properties, Size and BytesDownloaded, compute to give me the Progress property I've created in the partial class:

partial class Download
{
    public int Progress
    {
        get
        {
            if (!Size.HasValue || Size.Value == 0) return 0;
            return Convert.ToInt32(Math.Floor(100.0 * ((double)BytesDownloaded / (double)Size)));
        }
    }
}

在我的WPF UI我有

In my WPF UI I have:

<DataGridTemplateColumn x:Name="progressColumn" Header="Progress"  Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ProgressBar Value="{Binding Path=Progress, Mode=OneWay}" Maximum="100" />
       </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>



由于进步不是实体模型(EDMX)的一部分,我必须通知用户界面,它应更新进度。我想我能做到这一点,像这样:

Since Progress is not part of the Entity Model (edmx), I have to notify the UI that it should update the ProgressBar. I thought I could do this like so:

partial void OnBytesDownloadedChanging(long value)
{
    ReportPropertyChanging("Progress");
}
partial void OnBytesDownloadedChanged()
{
    ReportPropertyChanged("Progress");
} 

这编译很好,但是当我运行应用程序和OnBytesDownloadedChanging /更改被称为,我得到这个例外,我呼吁ReportPropertyChanging /更改:

This compiles fine, but when I run the app and OnBytesDownloadedChanging/Changed are called, I get this exception on my call to ReportPropertyChanging/Changed:

属性进度没有
A有效的实体映射对实体
对象。欲了解更多信息,请参阅
实体框架文档。

The property 'Progress' does not have a valid entity mapping on the entity object. For more information, see the Entity Framework documentation.

我明白了什么错误消息说,但我不' 。不懂我能做些什么来真正实现我的目标。

I understand what the error message is saying, but I don't understand what I can do to actually accomplish my goal.

PS - 什么具体的文档的他们甚至指?叹!如果他们将暗示有这种错误的文件,他们为什么不只是链接到我的文档,而不是告诉我的[无谓]试图找到它?

PS - What specific "documentation" are they even referring to? Sigh! If they're going to imply there is documentation for this error, why don't they just link me to the documentation instead of telling me to [pointlessly] try and find it?

推荐答案

使用 OnPropertyChanged /更改而不是 ReportPropertyChanged /不断变化。在开* 方法只能引发事件,而报告* 方法也标志着物业作为修改更改跟踪。

Use OnPropertyChanged/Changing instead of ReportPropertyChanged/Changing. The On* methods only raise the event, whereas the Report* methods also mark the property as modified for change tracking.

这篇关于显示计算属性的实体框架和WPF界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆