WPF:控制数据库/预渲染挂钩的可见性
我们有一个相当奇怪的要求:WPF 应用程序中任何控件的可见性都由数据库表驱动。
这意味着我们有一个表来存储元素的名称及其可见性。
现在我正在寻找一种优雅的方式来在客户端实现此功能。
我可以创建自己的 UserControl 并在任何地方继承它,提供一个 InitializeComponent 模板方法。但是如果有人以编程方式添加更多子控件怎么办?
我根本不想让我的控件知道这个机制。我想在某个点挂钩/拦截(预控件渲染)并根据数据库调整控件的可见性。
这有可能吗?如果没有,你会如何设计它?
we have a rather odd requirement: The visibility of any control in our WPF-Application is to be driven by a database-table.
That means we have a table which stores the name of the element and its visiblity.
Now I am looking for an elegant way to implement this feature on the client side.
I could create my own UserControl and inherit from it everywhere, providing a InitializeComponent Template Method. But what if someone programmatically adds more childcontrols?
I don't want my controls to know about this mechanism at all. I want to hook in/intercept at a certain point (pre control-rendering) and adjust the visibility of the control according to the database.
Is that somehow possible? And if not, how would you design it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这就是我所做的:
我从 UserControl 继承并将检查放入 Loaded 事件的 EventHandler 中。
然后我让我的控件继承自我的自定义控件,这效果很好。
由于某些我不知道的原因,FindName() 实现不会找到 FrameworkElements,尽管它们的名称是在 xaml 中定义的。所以这是我用来按名字查找孩子的例程:
我不喜欢这个解决方案,但它有效。
So here is what I did:
I inherited from UserControl and put the check in the an EventHandler for the Loaded Event.
I then let my Controls inherit from my CustomControl, this works pretty well.
The FindName() implementation won't find the FrameworkElements for some reason I don't know, although their names are defined in the xaml. So here is the routine I use to find the children by their names:
I don't like this solution but it works.