WPF:控制数据库/预渲染挂钩的可见性

发布于 2024-09-13 04:22:12 字数 292 浏览 3 评论 0原文

我们有一个相当奇怪的要求: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-09-20 04:22:12

所以这就是我所做的:

我从 UserControl 继承并将检查放入 Loaded 事件的 EventHandler 中。

然后我让我的控件继承自我的自定义控件,这效果很好。

由于某些我不知道的原因,FindName() 实现不会找到 FrameworkElements,尽管它们的名称是在 xaml 中定义的。所以这是我用来按名字查找孩子的例程:

    public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            string controlName = child.GetValue(Control.NameProperty) as string;

            if (controlName == name)
            {
                return child as T;
            }
            else
            {
                T result = FindVisualChildByName<T>(child, name);
                if (result != null)
                {
                    return result;
                }
            }
        }

        return null;
    }

我不喜欢这个解决方案,但它有效。

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:

    public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            string controlName = child.GetValue(Control.NameProperty) as string;

            if (controlName == name)
            {
                return child as T;
            }
            else
            {
                T result = FindVisualChildByName<T>(child, name);
                if (result != null)
                {
                    return result;
                }
            }
        }

        return null;
    }

I don't like this solution but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文