Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Hide control in parent page
13 replies. Latest Post by AveRus on November 5, 2009.
(0)
vaerge
Member
0 points
6 Posts
10-25-2009 8:26 PM |
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
Contributor
4717 points
595 Posts
10-25-2009 10:23 PM |
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
10-25-2009 10:33 PM |
Thanks - makes sense - but where and how do I create the property called 'parent'?
10-25-2009 10:52 PM |
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(); }}
jackbond
2820 points
725 Posts
10-27-2009 4:24 PM |
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; } }
10-28-2009 11:22 PM |
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).
Min-Hong...
3619 points
412 Posts
10-29-2009 2:42 AM |
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
AveRus
31 points
11 Posts
10-29-2009 3:12 AM |
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.
10-29-2009 9:45 PM |
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?
10-30-2009 5:13 AM |
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() { }
11-04-2009 8:45 AM |
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!
11-04-2009 9:57 AM |
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?
11-04-2009 10:08 AM |
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.
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.
11-05-2009 1:49 AM |
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!