在DataGrid(MVVM)中显示数据



请考虑以下Datamodel

由于我是新来的,所以我无法发布图像,所以我会尝试另一种方法...

作业实体-jobid - 乔布诺-jobstatus(状态实体的外键)-jobdate

状态实体-Statusid-Statuscaption

关系作业实体 * -------------------------------状态实体

我有一个WCF服务,该服务公开了我的JobsViewModel访问的模型

namespace PM.DataService
{
    [ServiceContract]
    public class PMService
    {
        [OperationContract]
        public ObservableCollection<Job> GetAllJobs()
        {
            using (var context = new logisticDBEntities())
            {
                var result = context.Jobs.ToList();
                result.ForEach(e => context.Detach(e));
                return new ObservableCollection<Job>(result);
            }
        }
        [OperationContract]
        public ObservableCollection<Status> GetStatuses()
        {
            using (var context = new logisticDBEntities())
            {
                var result = context.Statuses.ToList();
                result.ForEach(e => context.Detach(e));
                return new ObservableCollection<Status>(result);
            }
        }        
    }
}
namespace PM.UI.ViewModel
{
    public class JobsViewModel:INotifyPropertyChanged
    {
    private PMServiceClient serviceClient = new PMServiceClient();
    public JobsViewModel()
    {
        this.RefreshStatuses();
        this.RefreshAllJobs();
    }
    private void RefreshAllJobs()
    {
        this.serviceClient.GetAllJobsCompleted += (s, e) =>
        {
            this.allJobs = e.Result;
        };
        this.serviceClient.GetAllJobsAsync();
    }
    private void RefreshStatuses()
    {
        this.serviceClient.GetStatusesCompleted += (s, e) =>
        {
            this.Statuses = e.Result;
        };
        this.serviceClient.GetStatusesAsync();
    }
    private ObservableCollection<Job> allJobs;
    public ObservableCollection<Job> AllJobs
    {
        get{
            return this.allJobs;
        }
        set
        {
            this.allJobs = value;
            OnPropertyChanged("AllJobs");
        }
    }
    private ObservableCollection<Status> statuses;
    public ObservableCollection<Status> Statuses
    {
        get
        {
            return this.statuses;
        }
            set
            {
                this.statuses = value;
                this.OnPropertyChanged("Statuses");
            }
        }
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我在我的MainWindow的Xaml中包括了JobsViewModel

<Window x:Class="PM.FullClient.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:PM.UI"
    xmlns:vms="clr-namespace:PM.UI.ViewModel"
    Title="MainWindow" Height="475" Width="575">
 <Window.DataContext>
    <vms:JobsViewModel/>
 </Window.DataContext>
....

现在,我可以通过绑定以显示所有状态

来轻松地填充我的MainWindow
<DataGrid ItemsSource="{Binding Path=Statuses}" Margin="7,8,9,8" AutoGenerateColumns="True">

它有效,但是我无法显示输出cos,我无法在此处发布图像

但是,当我尝试在工作中做同样的事情时...什么都没有发生。数据杂志为空

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Path=AllJobs}" Margin="6">
</DataGrid>

我已经经历了许多搜索吨的四人和网站,最后我在这里。

是否可以是状态>工作与状态之间的工作关系?如果是这样,我该如何解决此问题,如果不是我在做错什么?

问题是在RefReshallJobs()回调中,您设置了 alljobs field(souter-case'a'),而不是 alljobs属性(上面的'a'),然后onpropertychanged()从不从属性 setter setter

最新更新