トップ回答者
ContextMenu表示後、ListBoxにおいてDragするとNullReferenceException

質問
-
お世話になっております。
WindowsXP VS2008 WPFアプリケーション
右クリックしてContextMenuを表示し、その後間髪を入れずにListBoxにおいてDragするとDoDragDropを呼び出した後で、
NullReferenceExceptionが発生してしまいます。
呼び出し元メソッド(PreviewMouseMove)を抜けるタイミングで発生するためcatchする事が出来ません。何か回避する方法
はありませんでしょうか。
.xamlソース-------------------------------------------------------------------------------------------------------------
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="269" Width="300">
<Grid Height="202">
<Label Height="103" Margin="119,0,20,16" Name="label1" VerticalAlignment="Bottom" AllowDrop="True">Label
<Label.ContextMenu>
<ContextMenu>
<MenuItem Header="MenuItem"/>
</ContextMenu>
</Label.ContextMenu>
</Label>
<ListBox Height="103" HorizontalAlignment="Left" Margin="18,0,0,12" Name="listBox1" VerticalAlignment="Bottom" Width="92" PreviewMouseLeftButtonDown="listBox1_PreviewMouseLeftButtonDown" PreviewMouseMove="listBox1_PreviewMouseMove"/>
</Grid>
</Window>
.csソース-------------------------------------------------------------------------------------------------------------
public partial class Window1 : Window{
// MouseDown Flag
bool isMouseDown = false;
// DragDrop String
string selectstring = null;
public Window1()
{
InitializeComponent();
listBox1.Items.Add("aaa");
listBox1.Items.Add("bbb");
}
private void listBox1_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown && selectstring != null && e.LeftButton == MouseButtonState.Pressed )
{
isMouseDown = false;
DragDrop.DoDragDrop(listBox1, selectstring, DragDropEffects.Move);
}
}
private void listBox1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UIElement element = (UIElement)listBox1.InputHitTest(e.GetPosition(listBox1));
selectstring = null;
while (true)
{
if (element == null || element == listBox1)
{
selectstring = null;
break;
}
object item = listBox1.ItemContainerGenerator.ItemFromContainer(element);
bool itemFound = !(item.Equals(DependencyProperty.UnsetValue));
if (itemFound)
{
selectstring = item as string;
break;
}
element = (UIElement)VisualTreeHelper.GetParent(element);
}
if (selectstring != null)
{
isMouseDown = true;
}
}
}
例外の詳細は以下の通りです。
よろしくお願いいたします。
-----------------------------------------------------------------------------------------------------------------------
System.NullReferenceException はハンドルされませんでした。
Message="オブジェクト参照がオブジェクト インスタンスに設定されていません。"
Source="PresentationCore"
StackTrace:
場所 System.Windows.Input.MouseDevice.ValidateVisualForCapture(DependencyObject visual)
場所 System.Windows.Input.MouseDevice.ReevaluateCaptureAsync(Object arg)
場所 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
場所 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
場所 System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
場所 System.Windows.Threading.DispatcherOperation.InvokeImpl()
場所 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
場所 System.Threading.ExecutionContext.runTryCode(Object userData)
場所 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
場所 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
場所 System.Windows.Threading.DispatcherOperation.Invoke()
場所 System.Windows.Threading.Dispatcher.ProcessQueue()
場所 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
場所 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
場所 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
場所 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
場所 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
場所 System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
場所 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
場所 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
場所 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
場所 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
場所 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
場所 System.Windows.Threading.Dispatcher.Run()
場所 System.Windows.Application.RunDispatcher(Object ignore)
場所 System.Windows.Application.RunInternal(Window window)
場所 System.Windows.Application.Run(Window window)
場所 System.Windows.Application.Run()
場所 WpfApplication1.App.Main() 場所 C:\user\xxxxxxx\WpfApplication1\WpfApplication1\obj\Debug\App.g.cs:行 0
場所 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
場所 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
場所 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
場所 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
場所 System.Threading.ThreadHelper.ThreadStart()
InnerException: