由于访问修饰符,子控件无法访问
当我在 WPF 中创建用户控件时,该控件的所有子控件似乎都被声明为内部控件?我无法验证这一点,也没有找到任何讨论此事的资源。
我可以访问同一程序集中的控件,但不能访问引用用户控件所在程序集的项目中的控件。有没有办法覆盖 XAML 中子控件的默认访问修饰符,这样我就不必通过用户控件中的属性手动公开控件?
When I create a user control in WPF it would appear that all the children of this control are declared as internal? I've not been able to verify this, nor finding any resources discussing this matter at all.
I can access the controls in the same assembly, but not in a project referencing the assembly where the user control is located. Is there a way to override the default access modifier of child controls in XAML so I don't manually have to expose the controls via properties in the user control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过设置
x:FieldModifier
您的子控件的属性为“public
”?Have you tried to set the
x:FieldModifier
attribute of your children controls to "public
"?然而,仅仅将它们公开并不是一个好主意——它的设计真的很糟糕。
您应该通过 UserControl 的属性公开它们的真实数据。最好的事情是引入一个接口。
另一种解决方案(更多的是“WPF 方式”)是使用数据上下文作为从外部访问的唯一属性:控件可以绑定到它们需要的属性。
However, just making them public, is no good idea - its really bad design.
You should expose their real data through properties of your UserControl. The best thing would be introducing an interface.
Another solution (more the "WPF-way") would be using the data-context as the only property which is accessed from outside: The controls could bind to the properties they need.