Context Menu in DataGridColumnHeader Style

Nov 20, 2008 at 6:15 PM
Hi,

Please can anybody tell me why this:

 

 

<Style x:Key="GridColumnHeader" TargetType="toolkit:DataGridColumnHeader">

 

 

 

 

<Setter Property="ContextMenu">

 

 

 

<Setter.Value>

 

 

 

<ContextMenu>

 

 

 

<MenuItem Header="xyz"/>

 

 

 

</ContextMenu>

 

 

 

</Setter.Value>

 

 

 

</Setter>

 

 

 

<Setter Property="Template">

 

 

 

<Setter.Value> ....... etc

 

doesn't work, it gives an error "Cannot add content of type 'System.Windows.Controls.ContextMenu' to an object of type 'System.Object'.  Error at object 'System.Windows.Controls.ContextMenu' in markup file"

when this does work:

 

 

<Style x:Key="GridColumnHeader" TargetType="toolkit:DataGridColumnHeader">

 

 

 

<EventSetter Event="ContextMenuOpening" Handler="columnHeader_ContextMenuOpening"/>

 

 

 

<Setter Property="Background" Value="{x:Null}"/>

 

 

 

<Setter Property="Foreground" Value="{StaticResource MGridColumnHeaderForeground}" />

 

 

 

<Setter Property="ContextMenu" Value="{StaticResource myCM}"/>

 

 

 

<Setter Property="ContextMenu">

 

 

 

</Setter>

 

 

 

<Setter Property="Template">

 

 

 

<Setter.Value>.... etc

 


Thanks
Coordinator
Nov 20, 2008 at 6:41 PM
This is not a problem with DataGridColumnHeaderStyle as is but a problem in general.

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1504cf4d-374c-4044-b409-f7e7f6b68add/ should provide a little more insight.
Nov 20, 2008 at 8:03 PM
Thanks, I think I understand that now. This brings me to my original problem: When I open a ContextMenu by clicking on the DataGridColumnHeader the ContextMenu flashes up for a fraction of a second, closes and then reopens and all is OK. If I click slowly (hold the right mouse button down for a second) then the menu only appears once and is OK. Also, if I click somewhere outside the Grid before I click the column header it is also OK. Any ideas why that might be?

I've tried removing the ContextMenuOpening event, that is not related.

Thanks

 

 

<Style x:Key="GridColumnHeader" TargetType="toolkit:DataGridColumnHeader">

 

 

 

<EventSetter Event="ContextMenuOpening" Handler="columnHeader_ContextMenuOpening"/>

 

 

 

<Setter Property="Background" Value="{x:Null}"/>

 

 

 

<Setter Property="Foreground" Value="{StaticResource MGridColumnHeaderForeground}"/>

 

 

 

<Setter Property="ContextMenu" Value="{StaticResource myCM}"/>

 

 

 

<Setter Property="Template">

 

 

 

<Setter.Value>

 

 

 

<ControlTemplate TargetType="{x:Type toolkit:DataGridColumnHeader}">.....

 


Coordinator
Nov 21, 2008 at 2:06 AM
I couldn't repro the behavior which you are reporting.
Nov 24, 2008 at 9:25 AM
Hi,

I was unable to find out what was wrong with the XAML, but by moving the creation of the context menu to code (ColumnHeader_ContextMenuOpening) the problem has gone away. If I remember I will try to debug this further sometime in the future.

Thanks for your help with the Style problem.