2012-01-17 4 views
1

J'ai une propriété construite dans un de mes ViewModels qui est un entier par rapport à une collection.Silverlight WCF/RIA MVVM Propriété

private int _formTypeID; 
    public int FormTypeID 
    { 
     get { return _formTypeID; } 

     set 
     { 
      if (_formTypeID == value) 
      { 
       return; 
      } 
      _formTypeID = value; 
      RaisePropertyChanged("FormTypeID"); 
     } 
    } 

Ce que je voudrais faire est de charger ceci à partir d'une requête de mon DomainContext. La requête ressemblerait à quelque chose comme:

public int GetFormTypeByForm(int Formid) 
    { 
     var p = (from i in this.ObjectContext.Forms 
       where i.FormID == Formid 
       select i.FormType).FirstOrDefault(); 

     return p; 
    } 

Mais je n'arrive pas à comprendre comment faire cela. J'utiliserai cette propriété pour charger des données spécifiques au formulaire et également charger des définitions de colonnes personnalisées dans mon code XAML.

Quelqu'un sait-il comment faire?

Merci,

Neil

Répondre

1

Avez-vous la forme parent présente dans une collection? Si oui, votre requête devrait fonctionner, utilisez simplement SingleOrDefault à la place.

Sinon, vous devez charger votre formulaire au DomainContext:

private int _FormType; 
public int FormType 
{ 
    get { return _FormType; } 
    set 
    { 
    if (_FormType == value) return; 
    _FormType = value; 
    RaisePropertyChanged("FormType"); 
    } 
} 

public void LoadFormTypeByForm(int Formid) 
{ 
    var query = (from f in this.ObjectContext.Forms 
       where f.FormID == Formid 
       select f.FormType); 

    var action = new Action<LoadOperation<Form>>((op) => 
    { 
     if (op.HasError && !op.IsErrorHandled) 
     { 
     op.MarkErrorAsHandled(); 
     return; 
     } 
     var form = ObjectContext.Forms.SingleOrDefault(f => f.FormID == FormID); 
     if (form != null) 
     FormType = form.FormType; 
    }); 
    Context.Load(query, action, null); 
} 
+1

C'est parfait! Merci! –