locked
Using a parent relation in a child detail screen RRS feed

  • Question

  • Hello,

    I have come across a strange issue with LightSwitch. I am attempting to use the relation to a parent entity in a child detail screen. The details are:

    I select a row from the parent screen and the child screen is shown, on the child screen I have a text field that contains the text (for example) 'Parent:' which I want top append the id of the parent to. In the Created method of the child screen I have the following code:

    partial void Child_Created()
            {
                this.FindControl("ParentIdText").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(UpdateParentIdText);
            }

    And then the UpdateParentIdText method is as follows:

    private void UpdateParentIdText(object sender, ControlAvailableEventArgs e)
    {
        FrameworkElement element = FrameworkElement)e.Control;
        if (this.Child.Parent != null)
        {
            System.Windows.Controls.TextBlock tb = (System.Windows.Controls.TextBlock)element;
            tb.Text = tb.Text + " " + this.Child.Parent.Id;
        }
    
        Utilities.SetDetailTitleText(element);
    }

    However, the Parent in the this.Child.Parent check is always null. A colleague of mine suggested simply using a data item by inserting other screen data which is an option, and when I did this the above relation check to the Parent was no longer null and so the above method started working!

    Any ideas?

    Cheers,

    Ken



    • Edited by kenjonesepa Wednesday, January 9, 2013 9:35 AM
    Wednesday, January 9, 2013 9:35 AM

Answers

  • I suggest that you use a Computed Property. Or in your Created method, just manipulate the property that your textbox is bound to.

    partial void Child_Created()
    {
        this.ParentIdText + " " + this.Child.Parent.Id;
    }

    And any modification of values should be performed on the properties that controls are bound to, not by trying to manipulate the contents of the controls directly.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by kenjonesepa Wednesday, January 9, 2013 4:03 PM
    Wednesday, January 9, 2013 2:56 PM
    Moderator

All replies

  • Hi Ken,

    I suspect the parent will always be null on the child unless your default query for he child fills its parent object - Im thinking  of a RIA service style setup (if you are using the EF model directly then my comment doesnt apply). Are you setting the parent in code on the screen 'data item by inserting other screen data' - sounds like you have the parent entity set on the screen or having to query it? Also what version of LS are you using and service (RIA or Odata)?

    Wednesday, January 9, 2013 2:26 PM
  • Dean,

    Thanks for your response. The data in my solution is coming from a SQL database. I am navigating from the parent screen to the child screen so I kind of assumed that the relationship would be there.

    In the last bit of my initial post I mentioned that if I include a field to display other screen data and pull in information from the parent then everything works so at the moment I am having to include a hidden field containing information from the parent just to make the relation in my code exist!

    Hope this all makes sense!

    Ken

    Wednesday, January 9, 2013 2:32 PM
  • I suggest that you use a Computed Property. Or in your Created method, just manipulate the property that your textbox is bound to.

    partial void Child_Created()
    {
        this.ParentIdText + " " + this.Child.Parent.Id;
    }

    And any modification of values should be performed on the properties that controls are bound to, not by trying to manipulate the contents of the controls directly.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by kenjonesepa Wednesday, January 9, 2013 4:03 PM
    Wednesday, January 9, 2013 2:56 PM
    Moderator
  • Thanks Yann, the computed property is the way forward I think.

    Wednesday, January 9, 2013 4:04 PM
  • You're welcome.

    The only disadvantage of computed properties is sometimes performance (if you're displaying a large number of records with one or more CP's, for example).

    Also remember, that the computed value isn't persisted to the database, unless you copy it to a "real" property.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 10, 2013 1:30 AM
    Moderator