如何在单击时检索控件的容器

时间:2012-11-07 09:21:05

标签: c# .net wpf xaml mvvm

我有TreeView,其中包含多个TreeViewItems。每个TreeViewItem都包含一个水平方向的StackPanel,而TreeViewItem又包含一个图像和一个标签。

当用户点击TreeViewItem时,我想获得TreeViewItem的参考。相反,源会以标签或图片的形式返回,具体取决于用户点击的<TreeView DockPanel.Dock="Left" Name="tvSchema" AllowDrop="True" TreeViewItem.PreviewMouseLeftButtonDown="tvSchema_PreviewMouseLeftButtonDown"> </TreeView> 部分。

我不能使用selectedItemChanged事件,因为这是一个必须在每次单击TreeViewItem时发生的操作。

这是我的XAML:

private void tvSchema_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     object x = e.OriginalSource; //Returns TextBlock
     object y = e.Source; //Returns Label
     object z = sender; //Returns TreeView
}

以下是当用户点击一个TreeViewItem时尝试检索所选TreeViewItem的代码:

{{1}}

如何获取实际的TreeViewItem?

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

<TreeView>
  <TreeView.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
      <EventSetter
        Event="TreeViewItem.PreviewMouseLeftButtonDown"
        Handler="MyHandler"/>
    </Style>
  </TreeView.Resrouces>
  ...
</TreeView>

MyHandler中,sender将是TreeViewItem

答案 1 :(得分:0)

这是助手类,希望它能帮到你:

public static class ElementTreeHelper
    {
        public static T FindParent<T>(this DependencyObject child) where T : DependencyObject
        {
            if (child == null)
                throw new ArgumentNullException("child");

            if (child is T)
                return child as T;

            var current = child;
            while (current != null)
            {
                if (current is Visual || current is Visual3D)
                    current = VisualTreeHelper.GetParent(current);
                else
                {
                    var previous = current;
                    current = LogicalTreeHelper.GetParent(previous) ?? previous.GetTemplatedParent();
                }

                if (current is T)
                    return current as T;
            }

            return null;
        }

        public static DependencyObject FindParent(this DependencyObject child, Type type)
        {
            if (child == null)
                throw new ArgumentNullException("child");

            if (type == null)
                throw new ArgumentNullException("type");

            if ((child.GetType().IsInstanceOfType(type)))
                return child;

            var current = child;
            while (current != null)
            {
                if (current is Visual || current is Visual3D)
                    current = VisualTreeHelper.GetParent(current);
                else
                {
                    var previous = current;
                    current = LogicalTreeHelper.GetParent(previous) ?? previous.GetTemplatedParent();
                }

                if (current != null 
                    && (current.GetType().IsInstanceOfType(type)))
                    return current;
            }

            return null;
        }

        public static DependencyObject GetTemplatedParent(this DependencyObject element)
        {
            DependencyObject parent = null;
            if (element is FrameworkElement)
                parent = (element as FrameworkElement).TemplatedParent;

            if (element is FrameworkContentElement)
                parent = (element as FrameworkContentElement).TemplatedParent;

            return parent;
        }

        public static Form FindForm(this DependencyObject element)
        {
            if (element == null)
                return null;

            var source = PresentationSource.FromDependencyObject(element) as HwndSource;
            if (source == null)
                return null;

            var host = Control.FromChildHandle(source.Handle) as ElementHost;
            if (host == null)
                return null;

            var form = host.TopLevelControl as Form;

            return form;
        }

        public static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject parent)
        {
            if (parent == null)
                throw new ArgumentNullException("parent");

            var count = VisualTreeHelper.GetChildrenCount(parent);
            if (count == 0)
            {
                DependencyObject child;
                if (parent.TryGetPropertyValue("Child", out child) && child != null)
                    yield return child;
                else
                {
                    IEnumerable children;
                    if (parent.TryGetPropertyValue("Children",out children) && children != null)
                    {
                        var dos = children.OfType<DependencyObject>();
                        foreach (var ch in dos)
                            yield return ch;
                    }
                }
            }

            for (var i = 0; i < count; i++)
                yield return VisualTreeHelper.GetChild(parent, i);
        }

        public static IEnumerable<DependencyObject> GetVisualChildrenSortedByTabIndex(this DependencyObject parent)
        {
            if (parent == null)
                throw new ArgumentNullException("parent");

            return parent.GetVisualChildren().OrderBy(KeyboardNavigation.GetTabIndex);
        }
    }