Skip to content

Commit

Permalink
fix for issue #59
Browse files Browse the repository at this point in the history
fix Exception when using WPF UserControl under ElementHost in an Excel AddIn.
if no window is found, search again for a UserControl.
  • Loading branch information
Jan Karger committed Jun 5, 2013
1 parent 1f58bdb commit 37ff740
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions GongSolutions.Wpf.DragDrop/DragDrop.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Xml;
using GongSolutions.Wpf.DragDrop.Icons;
using GongSolutions.Wpf.DragDrop.Utilities;
using System.Windows.Media.Imaging;
Expand Down Expand Up @@ -332,19 +328,8 @@ static void CreateDragAdorner()
{
DataTemplate template = GetDragAdornerTemplate(m_DragInfo.VisualSource);

UIElement rootElement = null;
Window parentWindow = m_DragInfo.VisualSource.GetVisualAncestor<Window>();
UIElement adornment = null;

if (parentWindow != null)
{
rootElement = parentWindow.Content as UIElement;
}
if (rootElement == null)
{
rootElement = (UIElement)Application.Current.MainWindow.Content;
}

if (template != null)
{
if (m_DragInfo.Data is IEnumerable && !(m_DragInfo.Data is string))
Expand Down Expand Up @@ -395,6 +380,20 @@ static void CreateDragAdorner()

if (adornment != null)
{
var parentWindow = m_DragInfo.VisualSource.GetVisualAncestor<Window>();
UIElement rootElement = parentWindow != null ? parentWindow.Content as UIElement : null;
if (rootElement == null && Application.Current != null && Application.Current.MainWindow != null) {
rootElement = (UIElement)Application.Current.MainWindow.Content;
}
// i don't want the fu... windows forms reference
// if (rootElement == null) {
// var elementHost = m_DragInfo.VisualSource.GetVisualAncestor<ElementHost>();
// rootElement = elementHost != null ? elementHost.Child : null;
// }
if (rootElement == null) {
rootElement = m_DragInfo.VisualSource.GetVisualAncestor<UserControl>();
}

DragAdorner = new DragAdorner(rootElement, adornment);
}
}
Expand Down

0 comments on commit 37ff740

Please sign in to comment.