0

DataGrid自動生成された列があります。これItemsSourceは、EFコンテキストへのLINQtoEntityクエリの結果です。列の1つには、エンティティのリストが含まれています。エンティティインスタンスの名前を表示するにはどうすればよいですか?

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を実装するRecipientオブジェクトのプロパティを作成します。

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には、自動生成列が使用できる名前のプロパティがあると想定しています。そうでない場合は、列を作成する必要があり、自動生成する必要はありません。

于 2012-11-11T16:18:01.287 に答える