Skip to main content

Microsoft Silverlight

Answered Question Hide control in parent pageRSS Feed

(0)

vaerge
vaerge

Member

Member

0 points

6 Posts

Hide control in parent page

 Hi all,

 I have a child page from which I would like invoke something that hides a control on the parent page - it's a HyperlinkButton I want to hide. 

The parent page is called MainPage.xaml. I tried this:

 

MainPage mainPage = new MainPage();
mainPage.linkButton1.Visibility = Visibility.Collapsed;
  

 - but this doesn't work.

What am I missing?

Thanks!

shamrat231
shamrat231

Contributor

Contributor

4717 points

595 Posts

Re: Hide control in parent page

Hi this will not work, u have to pass the reference of the parent page to the child page, not create an instance of the parent page on the fly which u did. Create a property called parent on the child page and assign the reference of the parent to this property. Then on child page

parent.linkButton1.Visibility = Visibility.Collapsed; will work

Sharker

 

 

Dhaka, Bangladesh
LinkedIn :: SL Profile :: Blog

vaerge
vaerge

Member

Member

0 points

6 Posts

Re: Re: Hide control in parent page

Thanks - makes sense - but where and how do I create the property called 'parent'?

 Thanks!

shamrat231
shamrat231

Contributor

Contributor

4717 points

595 Posts

Answered Question

Re: Re: Hide control in parent page

Its very simple actually, go through this tutorial, its just an example to clear your concept...nothing more

http://silverlight.net/learn/tutorials/multipageapps-cs/

Change it a litte here

void ChangePage_Click( object sender, RoutedEventArgs e )

   Page p = new Page();
   p.Parent = this;
   Switcher.Switch( p );

}

where in page.xaml.cs u add a property

public partial class Page : UserControl
{
    public UserControl Parent {get; set;} //in your case MainPage
   
    public Page()
    {
       InitializeComponent();
    }
}

Sharker

 

Dhaka, Bangladesh
LinkedIn :: SL Profile :: Blog

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Answered Question

Re: Hide control in parent page

shamrat231:
Create a property called parent on the child page and assign the reference of the parent to this property.

This is VERY BAD PRACTICE. Child controls should not know details of their parents for a variety of reasons. If an event in a child control occurs that should result in a parent updating its state, the child control should raise an event that the parent handles and then IT should update its own layout. To do otherwise reduces a control to nothing more than a collection of code instead of a re-usable self contained object. So, in the child control:

class TheChild
{
     public event EventHandler SomeNiftyThingHappened;

     void OnSomeNiftyThingHappened()
     {
          if(SomeNiftyThingHappened != null)
          {
                SomeNiftyThingHappened(this, null);
          }
     }
}

class TheParent
{
     TheChild _Child;

     public TheParent()
     {
           _Child = new TheChild();
           _Child.SomeNiftyThingHappened += TheHandler;
     }

     void TheHandler(object sender, EventArgs e)
     {
          linkButton1.Visibility = Visibility.Collapsed;
     }
}
 

vaerge
vaerge

Member

Member

0 points

6 Posts

Re: Hide control in parent page

Ok, this one is so much simpler - even I understood it...almost!

 How do I invoke this whole thing in the child? (sorry for any simplicity and ignorance in question level).

 Thanks!

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3619 points

412 Posts

Re: Hide control in parent page

Hi,

    I found a few nice and detailed articles of how to use custom event handler for you. Please check it out:

    http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers

    http://ondotnet.com/pub/a/dotnet/2002/04/15/events.html 

    http://www.csharphelp.com/archives2/archive470.html

Best Regards

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

AveRus
AveRus

Member

Member

31 points

11 Posts

Re: Hide control in parent page

vaerge:

Ok, this one is so much simpler - even I understood it...almost!

 How do I invoke this whole thing in the child? (sorry for any simplicity and ignorance in question level).

 Thanks!

Just call OnSomeNiftyThingHappened whenever it's necessary. This will fire the event. 

 

vaerge
vaerge

Member

Member

0 points

6 Posts

Re: Hide control in parent page

There's something I don't understand in this - and maybe why I can't get it to work: In TheParent() method I create a new instance of TheChild page. But... - it's not really this exact instance of TheChild page that I navigate to through my HyperlinkButton, so maybe that's why SomeNiftyThingHappened is null in TheChild. So if I'm right, I need to navigate to that exact page that is instantiated in TheParent() - but how do I do that? Or am I totally lost here?

AveRus
AveRus

Member

Member

31 points

11 Posts

Answered Question

Re: Hide control in parent page

class TheChild
{
     public event EventHandler SomeNiftyThingHappened;

     void OnSomeNiftyThingHappened()
     {
          if(SomeNiftyThingHappened != null)
          {
                SomeNiftyThingHappened(this, null);
          }
     }
}

class TheParent
{
     TheChild _Child;

     public TheParent()
     {
           _Child = new TheChild();
           _Child.SomeNiftyThingHappened += TheHandler;
     }

     void TheHandler(object sender, EventArgs e)
     {
          linkButton1.Visibility = Visibility.Collapsed;
     }
}


If I may use the above code to explain:

If you declared the child in your xaml file then yes the method TheParent (which is actually a constructor) will create a new object.

So if in your PARENT xaml file is something like this

<Child x:Name="myChild" SomeNiftyThingHappened="TheHandler">

</Child>

then you don't have to create another _child in your cs class. Your event SomeNiftyThingHappened of your child named myChild will then be handled by TheHandler.

Your parent c# code may be reduced to:

 class TheParent
{
     public TheParent()
     {
     }

     void TheHandler(object sender, EventArgs e)
     {
          linkButton1.Visibility = Visibility.Collapsed;
     }
}

vaerge
vaerge

Member

Member

0 points

6 Posts

Re: Hide control in parent page

Thanks, AveRus, I'm undestanding more and more ;-)

The thing is that in my parent xaml file uses 'navigation', so it doesn't directly reference the child. The parent pages has this:

  

    <Grid x:Name="LayoutRoot" Style="{StaticResource LayoutRootGridStyle}">

        <Border x:Name="ContentBorder" Style="{StaticResource ContentBorderStyle}">

            <navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}" 
                              Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
                <navigation:Frame.UriMapper>
                  <uriMapper:UriMapper>
                    <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
                    <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
                  </uriMapper:UriMapper>
                </navigation:Frame.UriMapper>
            </navigation:Frame>
        </Border>

        <Grid x:Name="NavigationGrid" Style="{StaticResource NavigationGridStyle}">

            <Border x:Name="BrandingBorder" Style="{StaticResource BrandingBorderStyle}">
                <StackPanel x:Name="BrandingStackPanel" Style="{StaticResource BrandingStackPanelStyle}">

                    <ContentControl Style="{StaticResource LogoIcon}"/>
                    <TextBlock x:Name="ApplicationNameTextBlock" Style="{StaticResource ApplicationNameStyle}" 
                               Text="KOM1 Controle"/>

                </StackPanel>
            </Border>

            <Border x:Name="LinksBorder" Style="{StaticResource LinksBorderStyle}">
                <StackPanel x:Name="LinksStackPanel" Style="{StaticResource LinksStackPanelStyle}">

                    <HyperlinkButton x:Name="Link1" Style="{StaticResource LinkStyle}" 
                                     NavigateUri="/Home" TargetName="ContentFrame" Content="home" />
									 
                    <Rectangle x:Name="Divider1" Style="{StaticResource DividerStyle}"/>
					
                    <HyperlinkButton x:Name="Link2" Style="{StaticResource LinkStyle}" 
                                     NavigateUri="/About" TargetName="ContentFrame" Content="about"/>

                    <Rectangle x:Name="Divider2" Style="{StaticResource DividerStyle}"/>

                    <HyperlinkButton x:Name="Link3" Style="{StaticResource LinkStyle}" 
                                     NavigateUri="/Calls" TargetName="ContentFrame" Content="calls"/>
                    
                    <Rectangle x:Name="Divider3" Style="{StaticResource DividerStyle}"/>

                    <HyperlinkButton x:Name="Link4" Style="{StaticResource LinkStyle}" 
                                     NavigateUri="/LoginPage" TargetName="ContentFrame" Content="Login"/>


                </StackPanel>
            </Border>

        </Grid>

    </Grid>
 

... - so how do I do it in this case?

 Thank you for your patience and good advice!

AveRus
AveRus

Member

Member

31 points

11 Posts

Re: Hide control in parent page

Can you tell me exactly what you are trying to do?

At what point dou you want to hide a hyperlink and which page is then visible in the navigation frame?

Where is the hyperlink located?

 

vaerge
vaerge

Member

Member

0 points

6 Posts

Re: Hide control in parent page

I login at one of the child pages, and depending on the role of the one who logs in, I want to show or hide controls (hyperlinks or other controls), which are on the parent page. The hyperlink (or other control) is located on the parent page, while the login that triggers whether it is shown in from a child page.

Thanks!

PS: Note that I don't even know if the right terminology is parent/child when we talk about these navigation pages in Silverlight - I just assume it is understandable.

AveRus
AveRus

Member

Member

31 points

11 Posts

Re: Hide control in parent page

I think you better create a static class to hold the login information, in this class you create events (just like the above child events) and some properties for your own rights.

You can handle events from this class in your mainpage, and fire them from anywhere you like, for example your child page.

In your mainpage you can bind the Visibility of your Hyperlink to a property of your static login class, here is an example for using a ValueConverter to bind to visibility:

http://www.jeff.wilcox.name/2008/07/visibility-type-converter/

Hope this helps!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities