2

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

当用户单击 TreeViewItem 时,我想获得一个参考TreeViewItem。取而代之的是,源作为标签或图像返回,具体取决于TreeViewItem用户单击的部分。

我不能使用 selectedItemChanged 事件,因为这严格来说是每次单击 TreeViewItem 时都必须发生的操作。

这是我的 XAML:

<TreeView DockPanel.Dock="Left" 
          Name="tvSchema" 
          AllowDrop="True"
          TreeViewItem.PreviewMouseLeftButtonDown="tvSchema_PreviewMouseLeftButtonDown">
</TreeView>

这是当用户单击一个时尝试检索所选 TreeViewItem 的代码:

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?

4

2 回答 2

2

尝试这样的事情:

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

MyHandlersender将是TreeViewItem

于 2012-11-07T09:56:17.797 回答
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);
        }
    }
于 2012-11-07T09:31:35.410 回答