4

今、これは価値があるよりも面倒かもしれませんが、それにもかかわらず、今のところ私にとって本当に役に立ちます.

私が知りたいのは、実行時に Silverlight ビジュアル ツリーを操作する方法です。コントロールの追加や削除などの単純なことを行うのは簡単ですが、適度に複雑なツリーをトラバースする必要がある場合は、DOM ノードの置換を処理するために JQuery スタイルの構文 (LINQ もかなりクールだと思います) が欲しくなります。 、動きなど。

問題は、これを簡単に行うためのライブラリが存在するか、それとも私が見逃した何かが組み込まれているかということだと思います。

4

4 に答える 4

6

はい、Linq拡張メソッドはあなたが求めているものですが、最初に小さなインフラストラクチャを配置する必要があります:-

public static class VisualTreeEnumeration
{
    public static IEnumerable<DependencyObject> Descendents(this DependencyObject root, int depth)
    {
        int count = VisualTreeHelper.GetChildrenCount(root);
        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(root, i);
            yield return child;
            if (depth > 0)
            {
                foreach (var descendent in Descendents(child, --depth))
                    yield return descendent;
            }
        }
     }

     public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)
     {
          return Descendents(root, Int32.MaxValue); 
     }

     public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
     {
          DependencyObject current = VisualTreeHelper.GetParent(root);
          while (current != null)
          {
              yield return current;
              current = VisualTreeHelper.GetParent(current);
          }
     }
 }

これで、Linq を使用して、Linq を使用してビジュアル ツリーにクエリを実行できるようになりました。いくつかの例:-

 // Get all text boxes in usercontrol:-
 this.Descendents().OfType<TextBox>();

 // All UIElement direct children of the layout root grid:-
 LayoutRoot.Descendents(0).OfType<UIElement>();

 // Find the containing `ListBoxItem` for an element:-
 elem.Ancestors().OfType<ListBoxItem>.FirstOrDefault();

 // Seek button with name "PinkElephants" even if outside of the current Namescope:-
 this.Descendents()
    .OfType<Button>()
    .FirstOrDefault(b => b.Name == "PinkElephants");
于 2010-12-13T13:13:20.587 に答える
2

このLINQ to Visual Tree の実装に興味があるかもしれません。

于 2010-12-13T12:56:47.893 に答える
0

これはどのバージョンのSilverlightですか?そして、この投稿は「12月13日13:13」の何年からですか?

SL4の現在のバージョンでは、存在しないようです。

于 2010-12-21T08:11:59.927 に答える
0

このコードを使用して、ビジュアル ツリーからコントロールを取得します

    public static FrameworkElement GetComponent(object child, Type t, Type bailOn)
    {
        if (child == null) return null;

        DependencyObject control = (DependencyObject)child; // VisualTreeHelper.GetParent((DependencyObject)x);

        while (control != null)
        {
            if (!control.Equals(child))
            {
                if (control.GetType() == t)
                {
                    break;
                }
            }

            if (control is FrameworkElement)
            {
                control = (control as FrameworkElement).Parent;
            }
            else if ((control is DataGridBoundColumn)) // data grid  fucken columns
            {
                control = GetDataGridBoundColumnDataGrid(control);

            }
            if (control != null && bailOn != null && bailOn.GetType() == control.GetType())
            {
                return null;
            }

        }

        // try VTH as we did not find it, as that works some times and the above does not
        if (control == null)
        {
            control = (DependencyObject)child; // start again

            while (control != null)
            {
                if (!control.Equals(child))
                {
                    if (control.GetType() == t)
                    {
                        break;
                    }
                }
                if (control is FrameworkElement)
                {
                    control = VisualTreeHelper.GetParent((control as FrameworkElement));
                }
                else if (control is DataGridBoundColumn)
                {
                    control = GetDataGridBoundColumnDataGrid(control);
                }

                if (control != null && bailOn != null && bailOn.GetType() == control.GetType())
                {
                    return null;
                }

            }

        }
        return control as FrameworkElement;
    }

    public static List<FrameworkElement> GetComponentsByType(FrameworkElement parent, Type type)
    {
        List<FrameworkElement> controls = new List<FrameworkElement>();
        GetComponentsByTypeWorker(parent, type, controls);
        return controls;

    }
    private static void GetComponentsByTypeWorker(FrameworkElement parent, Type type, List<FrameworkElement> controls)
    {

        if (parent.GetType() == type)
        {
            controls.Add(parent as FrameworkElement);
        }

        int cnt = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < cnt; i++)
        {
            FrameworkElement child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
            if (child.GetType() == type)
            {
                controls.Add(child as FrameworkElement);
            }

            int cnt2 = VisualTreeHelper.GetChildrenCount(child);
            for (int j = 0; j < cnt2; j++)
            {
                FrameworkElement child2 = VisualTreeHelper.GetChild(child, j) as FrameworkElement;

                GetComponentsByTypeWorker(child2 as FrameworkElement, type, controls);
            }
        }




    }
于 2014-05-20T05:07:07.103 に答える