Search code examples
silverlightmvvmwcf-ria-services

Silverlight WCF/RIA MVVM Property


I've got a Property built in one of my ViewModels that is an integer vs a collection.

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

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

What I'd like to do is to load this from a query from my DomainContext. The query would look something like:

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

        return p;
    }

But I can't seem to figure out how to do this. I will be using this property for loading form specific data and also loading custom column definitions into my XAML.

Anyone know how to do this?

Thanks,

Neil


Solution

  • Do you have the parent form present in a collection? If yes your query should work, just use SingleOrDefault instead.

    Otherwise, you have to load your form to the 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);
    }