0

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

加载的函数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人所属的所有内容。

4

1 回答 1

0

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

为实现 INotifyPropertyChanged 的​​收件人对象创建一个属性。

为您的 MailingList 添加另一个属性。让收件人属性也通知您的 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 具有自动生成列可以使用的名称的属性,否则您需要构建列而不是自动生成它们。

于 2012-11-11T16:18:01.287 回答