在WPF中访问Control.Parent.Parent属性 [英] Accessing Control.Parent.Parent Properties in WPF

查看:230
本文介绍了在WPF中访问Control.Parent.Parent属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧



i是WPF的新手,



i在Winforms中有一个项目使用了以下代码更改托管控件的父级父级的可见性:



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 :

Label1.parent.parent.visible=true



如何在WPF中做同样的事情?



(标签托管在StackPanel内部感谢所有人

更多信息:

在运行时填充父包装邮件通过stackpanels ..一个数据库表中的每个员工,每个员工由stackpanel内的两个标签表示,一个用于头像,另一个用于名称



当其中一个单击标签,其他堆栈面板代表其他员工隐藏,并显示该员工输入凭据的密码框

a来自winforms项目的更大代码示例:


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 :

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

推荐答案

这不是它在WPF中的工作方式。请参阅:

http:// msdn .microsoft.com / zh-CN / library / ms753391%28v = vs.110%29.aspx [ ^ ]。



你可以遍历两棵树在不同的事件上,但到目前为止在WPF中并不那么容易。我已经为真正的,非常先进的和普遍的行为做了两件事。对于一个应用程序,我不建议打扰。您可以在XAML中简单地提供所需的特定UI元素,并按名称访问它。如果你对它有一些特别的关注,请清楚地解释你为什么这样做的想法,你的最终目标是什么。然后就可以想到别的了。



-SA
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


说真的,即使在winforms这将是一个糟糕的,糟糕的想法 - 它打破了OOPS的所有规则,因为它使得控制必须意识到它的父母存在,以及父母的存在。所以你不能在任何地方重复使用这个控件,除非不会失败。



坏主意。



相反,每个控件都应该引发一个事件,该事件由更高级别的控件处理(或者不是,它是他们的选择),最终隐藏了你想要的面板 - 但是包含的控件不需要知道它们存在的位置在层次结构中。
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.


这篇关于在WPF中访问Control.Parent.Parent属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆