Click here to Skip to main content
15,894,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

i am new to WPF,

i had a project in Winforms that used the following code to change the visibility of the parent's parent of a hosted control :

VB
Label1.parent.parent.visible=true

how can i do the same thing in WPF ?

(the label is hosted inside a StackPanel which in turn is hosted in a wrapPanel

thanks everyone
more info :
the parent wrappanel is populated at run time by stackpanels .. one for each employee in a database table,each employee is represented by two labels inside the stackpanel one for avatar and other for name

when one of the labels is clicked the other stackpanels representing other employees are hidden and a password box appears for that employee to enter credentials
a larger code sample from the winforms project :
VB
Sub labelclick(ByVal sender As Object, ByVal e As EventArgs)
      If txtPassword.Visible Then Exit Sub
      reverseVisibility()
      For Each c As Control In pnl.Controls
          c.Visible = False
      Next
      With CType(sender, Control)
          .Parent.Visible = True
          pass = ds.Employees.FindByEmployeeID(.Tag).PassWord
          EID = .Tag
      End With
      If pass = "" Then
          With ds.Employees.FindByEmployeeID(EID)
              My.Settings.EmployeeID = .EmployeeID
              My.Settings.EmployeeName = .EmployeeName
              My.Settings.Privilage = .PrivilageType
          End With

          frmBase.Show()
          Me.Close()
      End If
      txtPassword.Focus()
  End Sub
Posted
Updated 6-Mar-14 8:38am
v2

This is not how it works in WPF. Please see:
http://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx[^].

You can traverse both trees on different events, but it is by far no so easy to do in WPF. I have done both things for really, really advanced and universal behavior. For an application, I would not recommend to bother. You can simply give the required particular UI element in XAML and access it by name. If you have some special concern about it, please clearly explain your idea on why doing so, what is your ultimate goal. Then would be able to think of something else.

—SA
 
Share this answer
 
Comments
Ahmad_kelany 6-Mar-14 14:57pm    
thanks very much
that was really helpful
i glanced at the article , but i will read it more carefully after i go home from work
thanks again
Sergey Alexandrovich Kryukov 6-Mar-14 15:07pm    
Great. You are very welcome.
Good luck, call again.
—SA
Seriously, even in winforms this would be a poor, poor idea - it breaks all the rules of OOPS in that it makes the control have to be aware of it's parents existence, and the existence of a parent for that as well. So you can't re-use this control anywhere except where that won't fail.

Bad idea.

Instead, each control should raise an event which is handled by a higher level control (or not, it's their choice) which ends up hiding the panel you want to - but the contained controls do not need to know where they exist in the hierarchy.
 
Share this answer
 
Comments
Ahmad_kelany 6-Mar-14 14:59pm    
thanks very much
my mind was so winform oriented that it completely passed my mind about bubbling events
so the solution for me was to handle the click event of the parent-most container directly

thanks again
Sergey Alexandrovich Kryukov 6-Mar-14 15:09pm    
I appreciate that you are able to detect the trend and are going to overcome it and think at better solutions.
I confirm the the idea of bubbling the event is much more healthy.
—SA
OriginalGriff 6-Mar-14 15:22pm    
You're welcome!
Sergey Alexandrovich Kryukov 6-Mar-14 15:08pm    
I have to agree; good idea, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900