从 viewModel 获取控件

发布于 2024-11-27 14:18:37 字数 463 浏览 0 评论 0原文

在我的 viewModel 中,我可以使用该窗口中有一个 FlowDocument 来获取与其关联的窗口

var windows = Application.Current.Windows;
            for (var i = 0; i < windows.Count; i++)
            {
                if (windows[i].DataContext == this)
                {
                   //                      
                }
            }

,我需要在我的 viewModel 中引用它,我知道我有时可以打破规则并编写一些代码隐藏,但因为我有窗口和此控件包含在该窗口中/子窗口中,我想我可以在没有任何代码隐藏的情况下完成它,有什么建议吗?

提前致谢

in my viewModel I can get the window associated with it using

var windows = Application.Current.Windows;
            for (var i = 0; i < windows.Count; i++)
            {
                if (windows[i].DataContext == this)
                {
                   //                      
                }
            }

there is a FlowDocument in this window that I need a reference to it here in my viewModel, I know I can sometime break the rules and write some code-behind , but since I have the window and this control is contained/child in/of this window I thought I can get it done without aany code-behind , any suggesstions ?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半﹌身腐败 2024-12-04 14:18:38

首先,如果您需要访问 ViewModel 中的 UI 元素,那么您就没有以正确的方式使用 MVVM。您应该考虑为此使用绑定(无论您正在做什么:-)

无论如何,您可以遍历可视化树来查找 Window 的后代。但是,FlowDocument 不在可视化树中,因为它是 FrameworkContentElement,因此 VisualTreeHelper 将不起作用。

您需要结合 VisualTreeHelperLogicalTreeHelper:可以在此处找到此实现:通过视觉树查找元素

这是它的一个稍微重写的版本,使用它就像

if (windows[i].DataContext == this)
{
    var flowDocument = windows[i].FindChild<FlowDocument>();
}

DependencyObjectExtensions.cs

public static class DependencyObjectExtensions
{
    public static T FindChild<T>(this DependencyObject source) where T : DependencyObject
    {
        if (source != null)
        {
            var childs = GetChildObjects(source);
            foreach (DependencyObject child in childs)
            {
                //analyze if children match the requested type
                if (child != null && child is T)
                {
                    return (T)child;
                }

                T descendant = FindChild<T>(child);
                if (descendant is T)
                {
                    return descendant;
                }
            }
        }
        return null;
    }
    public static IEnumerable<DependencyObject> GetChildObjects(this DependencyObject parent)
    {
        if (parent == null) yield break;

        if (parent is ContentElement || parent is FrameworkElement)
        {
            //use the logical tree for content / framework elements
            foreach (object obj in LogicalTreeHelper.GetChildren(parent))
            {
                var depObj = obj as DependencyObject;
                if (depObj != null) yield return (DependencyObject)obj;
            }
        }
        else
        {
            //use the visual tree per default
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                yield return VisualTreeHelper.GetChild(parent, i);
            }
        }
    }
}

First of, you're not using MVVM the right way if you need access to UI elements in the ViewModel. You should consider using bindings for this instead (whatever it is that you're doing:-)

Anyway, you can traverse the visual tree to find descendants of the Window. However the FlowDocument isn't in the visual tree as it is a FrameworkContentElement so VisualTreeHelper won't work.

You'll need to combine VisualTreeHelper and LogicalTreeHelper: An implementation of this can be found here: Find Element By Visual Tree

Here is a slightly rewritten version of it, use it like

if (windows[i].DataContext == this)
{
    var flowDocument = windows[i].FindChild<FlowDocument>();
}

DependencyObjectExtensions.cs

public static class DependencyObjectExtensions
{
    public static T FindChild<T>(this DependencyObject source) where T : DependencyObject
    {
        if (source != null)
        {
            var childs = GetChildObjects(source);
            foreach (DependencyObject child in childs)
            {
                //analyze if children match the requested type
                if (child != null && child is T)
                {
                    return (T)child;
                }

                T descendant = FindChild<T>(child);
                if (descendant is T)
                {
                    return descendant;
                }
            }
        }
        return null;
    }
    public static IEnumerable<DependencyObject> GetChildObjects(this DependencyObject parent)
    {
        if (parent == null) yield break;

        if (parent is ContentElement || parent is FrameworkElement)
        {
            //use the logical tree for content / framework elements
            foreach (object obj in LogicalTreeHelper.GetChildren(parent))
            {
                var depObj = obj as DependencyObject;
                if (depObj != null) yield return (DependencyObject)obj;
            }
        }
        else
        {
            //use the visual tree per default
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                yield return VisualTreeHelper.GetChild(parent, i);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文