What's wrong with the ContextMenu for RowHeader

Aug 10, 2009 at 7:53 AM
Edited Aug 10, 2009 at 7:56 AM
Hi experts,
 
I hit the problem when I want to add ContextMenu for the RowHeader when I use the resource from ResourceDictionary. What's wrong with my code? 
I found when I remove the Click event handler, it's fine. But the context menu will do nothing.
So is there any way to make it work or how can I get the ContextMenu of RowHeader by code then I will add event handler manually?
 
l:BaseDataGrid is derived from DataGrid.
Thanks very much
        <l:BaseDataGrid.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="../Resources/PointCloudUIStyle.xaml"/>
                </ResourceDictionary.MergedDictionaries>
                <ContextMenu x:Key="RowHeaderContextMenu" >
                    <MenuItem x:Uid="PC_001" Header="Move Up" IsCheckable="False" Click="MoveUp_MenuItem_Click"/>
                    <MenuItem x:Uid="PC_002" Header="Move Down" IsCheckable="False" Click="MoveDown_MenuItem_Click"/>
                    <Separator/>
                    <MenuItem x:Uid="PC_003" x:Name="Reomve_from_Group_MenuItem" Header="Remove from Group" Click="btnRemoveFromGroup_Click" IsEnabled="{Binding Path=IsFileInGroup}"/>
                    <MenuItem x:Uid="PC_004" x:Name="Reomve_File_MenuItem" Header="Remove File" Click="btnRemoveFile_Click"/>
                </ContextMenu>
                <Style x:Key="ControlRowHeaders" TargetType="{x:Type dg:DataGridRowHeader}">
                    <Setter Property="Content" Value="{Binding Path=RowHeaderImage}"/>
                    <Setter Property="Template" Value="{DynamicResource PCRowHeaderControlTemplate}"/>
                    <Setter Property="ContextMenu" Value="{StaticResource RowHeaderContextMenu}"/>
                </Style>
                <Style TargetType="{x:Type dg:DataGridRowHeader}" BasedOn="{StaticResource ControlRowHeaders}"/>
            </ResourceDictionary>
        </l:BaseDataGrid.Resources>
          
The statck trace is really strange:

at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)

   at System.Windows.Markup.BamlRecordReader.ReadElement(Int64 startPosition, XamlObjectIds contextXamlObjectIds, Object dictionaryKey)

   at System.Windows.ResourceDictionary.CreateObject(Int32 valuePosition, Object key)

   at System.Windows.ResourceDictionary.RealizeDeferContent(Object key, Object& value, Boolean& canCache)

   at System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache)

   at System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache)

   at System.Windows.DeferredResourceReference.GetValue(BaseValueSourceInternal valueSource)

   at System.Windows.DependencyObject.GetEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, RequestFlags requests)

   at System.Windows.DependencyObject.GetValueEntry(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, RequestFlags requests)

   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)

   at System.Windows.Controls.ContextMenuService.GetContextMenu(DependencyObject element)

   at System.Windows.Controls.ContextMenuService.ContextMenuIsEnabled(DependencyObject o)

   at System.Windows.Controls.ContextMenuService.OnContextMenuOpening(Object sender, ContextMenuEventArgs e)

   at System.Windows.Controls.ContextMenuEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)

   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)

   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)

   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)

   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)

   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)

   at System.Windows.Controls.PopupControlService.RaiseContextMenuOpeningEvent(IInputElement source, Double x, Double y, Boolean userInitiated)

   at System.Windows.Controls.PopupControlService.ProcessMouseUp(Object sender, MouseButtonEventArgs e)

   at System.Windows.Controls.PopupControlService.OnPostProcessInput(Object sender, ProcessInputEventArgs e)

   at System.Windows.Input.InputManager.ProcessStagingArea()

   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)

   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)

   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)

   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)

   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)

   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)

   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)

   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)

   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)

   at System.Windows.Forms.NativeWindow.WndProc(Message& m)

   at System.Windows.Forms.Integration.WindowsFormsHost.ActivateWindowListener.WndProc(Message& m)

   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

               

Aug 11, 2009 at 2:27 AM

Oh, I solved it by remove the

<Style TargetType="{x:Type dg:DataGridRowHeader}" BasedOn="{StaticResource ControlRowHeaders}"/>

And remove x:Key="ControlRowHeaders" from the code below:

<Style x:Key="ControlRowHeaders" TargetType="{x:Type dg:DataGridRowHeader}">

Thanks still