如何显示List<> DataGrid单元格中的项目?

时间:2012-11-11 15:24:56

标签: c# wpf entity-framework datagrid

我有DataGrid个自动生成的列。 ItemsSource是对EF上下文进行LINQ to Entity查询的结果。其中一列包含实体列表。如何让它显示实体实例的名称?

加载DataGrid

的函数
    private void DataGridRecipientsLoad()
    {
        List<Recipient> recipients = _recipientService.GetAllRecipients();
        dataGridRecipients.ItemsSource = from rec in recipients select rec;
        if (recipients.Count() == 0) return;
        dataGridRecipients.Columns[7].Visibility = System.Windows.Visibility.Collapsed;
        dataGridRecipients.Columns[8].Visibility = System.Windows.Visibility.Collapsed;
    }

Recipient定义:

[EdmEntityTypeAttribute(NamespaceName="NewsletterModel", Name="Recipient")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Recipient : EntityObject
    {
    #region Factory Method

    /// <summary>
    /// Create a new Recipient object.
    /// </summary>
    /// <param name="recipientID">Initial value of the RecipientID property.</param>
    /// <param name="firstName">Initial value of the FirstName property.</param>
    /// <param name="email">Initial value of the Email property.</param>
    public static Recipient CreateRecipient(global::System.Int32 recipientID, global::System.String firstName, global::System.String email)
    {
        Recipient recipient = new Recipient();
        recipient.RecipientID = recipientID;
        recipient.FirstName = firstName;
        recipient.Email = email;
        return recipient;
    }

    #endregion

    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 RecipientID
    {
        get
        {
            return _RecipientID;
        }
        set
        {
            if (_RecipientID != value)
            {
                OnRecipientIDChanging(value);
                ReportPropertyChanging("RecipientID");
                _RecipientID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("RecipientID");
                OnRecipientIDChanged();
            }
        }
    }
    private global::System.Int32 _RecipientID;
    partial void OnRecipientIDChanging(global::System.Int32 value);
    partial void OnRecipientIDChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            OnFirstNameChanging(value);
            ReportPropertyChanging("FirstName");
            _FirstName = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("FirstName");
            OnFirstNameChanged();
        }
    }
    private global::System.String _FirstName;
    partial void OnFirstNameChanging(global::System.String value);
    partial void OnFirstNameChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String LastName
    {
        get
        {
            return _LastName;
        }
        set
        {
            OnLastNameChanging(value);
            ReportPropertyChanging("LastName");
            _LastName = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("LastName");
            OnLastNameChanged();
        }
    }
    private global::System.String _LastName;
    partial void OnLastNameChanging(global::System.String value);
    partial void OnLastNameChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String City
    {
        get
        {
            return _City;
        }
        set
        {
            OnCityChanging(value);
            ReportPropertyChanging("City");
            _City = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("City");
            OnCityChanged();
        }
    }
    private global::System.String _City;
    partial void OnCityChanging(global::System.String value);
    partial void OnCityChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String Email
    {
        get
        {
            return _Email;
        }
        set
        {
            OnEmailChanging(value);
            ReportPropertyChanging("Email");
            _Email = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("Email");
            OnEmailChanged();
        }
    }
    private global::System.String _Email;
    partial void OnEmailChanging(global::System.String value);
    partial void OnEmailChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String Phone
    {
        get
        {
            return _Phone;
        }
        set
        {
            OnPhoneChanging(value);
            ReportPropertyChanging("Phone");
            _Phone = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("Phone");
            OnPhoneChanged();
        }
    }
    private global::System.String _Phone;
    partial void OnPhoneChanging(global::System.String value);
    partial void OnPhoneChanged();

    #endregion


    #region Navigation Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("NewsletterModel", "RecipientMailingList", "MailingList")]
    public EntityCollection<MailingList> MailingList
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<MailingList>("NewsletterModel.RecipientMailingList", "MailingList");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<MailingList>("NewsletterModel.RecipientMailingList", "MailingList", value);
            }
        }
    }

    #endregion

}

我想显示每个MailingLists所属的所有Recipient

1 个答案:

答案 0 :(得分:0)

创建一个包含Recipient对象的ViewModel。

为实现INotifyPropertyChanged的

为您的MailingList添加另一个属性。让Recipient属性也通知您的MailingList已更改属性。

public Recipient Recipient
{
    get { return _Recipient; }
    set
    {
        _Recipient = value;
        NotifyPropertyChanged("Recipient");
        NotifyPropertyChanged("MailingList");
    }
} private Recipient _Recipient

public EntityCollection<MailingList> MailingList
{
    get { return _MailingList; }
    set
    {
        _MailingList= value;
        NotifyPropertyChanged("MailingList");
    }
} private EntityCollection<MailingList> _MailingList

然后在您的视图中,我假设您有某种方式来选择个人或收件人,当您选择它们​​时,收件人会更改。然后应该存在另一个显示邮件列表的视图。然后将DataGrid的ItemsSource绑定到MailingList属性。

我假设EntityCollection是IList或IEnumerable,否则,您可能必须将其转换为类似List或ObservableCollection的东西。如果需要,创建一个MailingListViewModel并在必要时创建List或ObservableCollection。

另外,我假设MailingList具有自动生成列可以使用的名称的属性,否则您需要构建列而不是自动生成它们。