Skip to main content

Microsoft Silverlight

Answered Question Help with wiring up parent and childrenRSS Feed

(1)

donmarais
donmarais

Member

Member

37 points

82 Posts

Help with wiring up parent and children

I'll explain this in simple terms: I have a UI with various controls, of which contains a menu with links. Above the menu is a display area (grid). As the user clicks on each link, the display area populates with the links' content. I am trying to wire it all together so that when a link is clicked, the open content will first close and then the new content will open. My logic tells me that I should write a class for the display area and set each of the links' contents as children. When a child is called, the display area (or grid, aka parent) checks to see if a child is present (irrespective of the child's identity), and then closes that child and loads the new child. Easier said than done. Can anyone offer me any assistance?

 

 

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

microsoft_kc
microsof...

Contributor

Contributor

2864 points

561 Posts

Re: Help with wiring up parent and children

You may try by storing the reference in a variable.

 

Remember: Please click on "Mark As Answer", if this answered your query partially or fully.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Help with wiring up parent and children

Hi,

I think you Can add Dynamically some control like TextBlock to Your Grid and you Can populate that with the content when link is cliked.

Again i9f lik is clicked st that time you Can check that Grid is having any children or not(I am assuming Grid is not having any children and after adding TextBlock it is having a children.You Can customize it accordingly for e.g you can loop throgh grid child to check TextBlock ) and athen you Can Remove it at First if TextBlock is Present and then after that you can add a TextBlock object and populate with links content.

you Can repeat this thing on every call.

You Can try with code like this-

If(myGrid.Children.Count>0)

{

myGrid.children.Clear();

TextBlock tb=new TextBlock();

tb.Text="Link Relative Data";

myGrid.Children.Add(tb)

}

}

You Can other control even user control also according to your need.

Mark this as answer if it helps..

 

Mark as answer if this post answered your question.

Harsh Bardhan

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Help with wiring up parent and children

I appreciate your help. Your idea sounds good.

I have created a grid and inserted a ContentControl (called myControl) to use to hold each child object. For some reason, when trying to call the children from the event handler, it won't recognize "myControl.Children.Count > 0".

Error mesage: System.Windows.Controls.ContentControl does not contain a definition for Children.

It looks like I should use "myControl.Content" instead.

Would it be better to use contentControl for displaying multiple objects or should just display the objects as children to the grid?

Each object has a slide-up animation and the grid has a clipping mask(path) for viewing the object.

 

 

 

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Help with wiring up parent and children

I went ahead and removed the ContentControl and left a TextBlock in the display grid (contentArea).

 In the code, I changed the event handler for one of the links:

private void AboutUsEvent(object sender, RoutedEventArgs e)

{

if (contentArea.Children.Count > 0)

{

contentArea.Children.Clear();

}

}

 After building, when the About Us link is pressed, the TextBlock is cleared from display.

 Now I need to call the AboutUs.xaml page to display in the display grid (contentArea).

 Can you help me to call the child element from the event handler?

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Help with wiring up parent and children

Hi,

you can try something like this.

private void AboutUsEvent(object sender, RoutedEventArgs e)

 { if (contentArea.Children.Count > 0)

 {

contentArea.Children.Clear();

}

 AboutUs as=new Aboutus();

//If there is a property by which you want to see some text inside that you can do here ....

//then add it like this..

 contentArea.Children.Add(as);

 Hope it will help. Thanks,,

Mark as answer if this post answered your question.

Harsh Bardhan

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Re: Help with wiring up parent and children

Does it matter that the AboutUs.xaml page that I'm calling is a seperate file and not within the page.xaml file?

 For some reason I'm battling to call it.

 I tried your code, but it didn't work. I also tried the following:

private void AboutUsEvent(object sender, RoutedEventArgs e)

{

if (contentArea.Children.Count > 0)

{

contentArea.Children.Clear();

}

else

{

contentArea.Children.Add(AboutUsContent());

}

}

 

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Help with wiring up parent and children

I think that makes sense.

I have my page.xaml, which has the main display grid. I have additional xaml pages for each successive page. I am trying to call each xaml page to display in the main display grid.

Should I create a class for each xaml object and reference it from the event handler in the page.xaml.cs page?

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Re: Help with wiring up parent and children

Do I not need to add code to the AboutUs.xaml.cs page as well?

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Re: Help with wiring up parent and children

Hi,

If you will create any Xaml Page By default it will create a Class for you..

You Can add some control in that UserControl(AboutUs.xaml).

If you want to process some thing for those controls you Can Add Code also.

In case you Don't want to process any thing in that USERControl(Here AboutUs.xaml)  You Simply want a UI then i think no need(But i don't think this case is ever true).

Now According to my understanding here You have creates So many Controls and in Your Page.xaml you are having Some links for each control (like about us,home etc)......

You Can create a StackPanel or Canvas(Main Purpose of this is to provide a container for your Control) inside that Grid.

Don't add any other thing inside that ..

So it will look like this...

<Grid x:Name="layoutRoot">

<!--Other thing goes here-->

<Canvas x:Name="mainCanvas>

</Canvas>

<<!--Other thing goes here-->

</Grid>

Now on click of Aboutus link You Want to Remove other things  and  add about us Control.

On click of Other Link(like Home for e.g) you want to Remove other things  and  add that particular control there(Home here for e.g).

you Can write individual hyandlers for that .

In aboutus click handler add AboutUs object to Canvas or StackPanel and in case of Home add Home OPbject..

Before adding anything you clear Your Canvas or Stack Panel..

 

private void AboutUsEvent(object sender, RoutedEventArgs e)

 {

if (mainCanvas.Children.Count > 0)

 {

mainCanvas.Children.Clear();

 }

AboutUs as=new Aboutus();

//If there is a property by which you want to see some text inside that you can do here ....

 //then add it like this..

mainCanvas.Children.Add(as);

 }

Mark as answer if this post answered your question.

Harsh Bardhan

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Re: Help with wiring up parent and children

You are correct in your explanation of the intention. Many others will benefit from our discussions, as I am sure this is a common question.

 Some points to go through what I have learnt:

1. I have a page.xaml as my starter page within my solution, called SeafireGrillApp.

2. To cut down page size, I have additional xaml pages for each additional page.

3. The page.xaml has a menu with links, e.g. About Us (AboutUsContent.xaml), Contact Us, Locate Us, etc.

4. The page.xaml has a canvas where each link's content will be displayed. Canvas is called "displayArea".

5. In page.xaml.cs, I wrote an event handler for the mouse click for each link, e.g:

 

private void AboutUs(object sender, MouseButtonEventArgs e)

{

if (displayArea.Children.Count > 0)

{

displayArea.Children.Clear();

}

//

// I refer to the AboutUsContent.xaml page through the SeafireGrillApp root name. 

// 

AboutUsPage as = new SeafireGrillApp.AboutUsContent();

displayArea.Children.Add(as);

}

 

6. In AboutUsContent.xaml, I have an animation for the content to slide up into view within the canvas(displayArea):

<UserControl.Resources>

<Storyboard x:Name="AboutUs">

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="About_Us" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="230"/>

<SplineDoubleKeyFrame KeyTime="00:00:03" Value="0"/>

</DoubleAnimationUsingKeyFrames>

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>

<SplineDoubleKeyFrame KeyTime="00:00:03" Value="0"/>

</DoubleAnimationUsingKeyFrames>

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>

<SplineDoubleKeyFrame KeyTime="00:00:03" Value="0"/>

</DoubleAnimationUsingKeyFrames>

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>

<SplineDoubleKeyFrame KeyTime="00:00:03" Value="0"/>

</DoubleAnimationUsingKeyFrames>

</Storyboard>

</UserControl.Resources>

 

7. In App.xaml.cs, I changed the start up as follows:

 

private void Application_Startup(object sender, StartupEventArgs e)

{

Grid root = new Grid();

root.Children.Add(new Page());

this.RootVisual = root;

}

 

In theory this should work, but for some reason, it won't build because "as" is an invalid expression term and "=" type expected.

AboutUsPage as = new SeafireGrillApp.AboutUsContent();

displayArea.Children.Add(as);

 

I know I am so close. So as such this post remains unresolved. In another version, I inserted all the links' content as seperate animations for each object called upon using event handlers on the links. Each animation fires correctly, but I was concerned about the final page size and how this would negatively effect the browser experience. Therefore, I thought it best to create seperate xaml pages within my solution who's content can be displayed in the main page, thus enabling only that file to be fired, and thus dramatically cutting back on page size.

Any ideas?

 

 

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Answered Question

Re: Re: Help with wiring up parent and children

Hi.

as is a Predefined keyword...

You Can try with somethingh like this... 

AboutUsPage asa = new SeafireGrillApp.AboutUsContent();

displayArea.Children.Add(asa);

Cheers...

Mark as answer if this post answered your question.

Harsh Bardhan

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Re: Help with wiring up parent and children

To anyone reading this post:

I was successfull in getting the issue resolved. While I am a junior developer, I wish to share this learning experience with you. I would like to point out some things for you to take not of when you work on your own projects:

1. If you create seperate xaml pages, keep that pages' animations and code together and not in your main xaml page.

2. Be sure to add enough comments as you write code. That way you speed up debugging and can quickly identify the xaml code that needs changing.

3. When referring to the xaml pages that you wish to link to from the main xaml page, be sure to reference it through the solution root.

4. In order to link to the xaml pages, create a reference in your class file in your main.xaml.cs page to the page to call.

I hope this post will benefit you and if so, please be sure to come back and share your experience so that we all can benefit.

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Re: Help with wiring up parent and children

Thank you to Harsh Bardhan for all his help.

If we as the development community can stand together and help one another, we can make Silverlight a success.

There you go, that's my shpiel for the movement!

 

 

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities