I want to create a ContectMenu in my XAML file, but display it from within my code-behind. That is, I do not want the ContextMenu to capture the event and display itself. I want to programatically display the ContectMenu. Any way to do this?
One recommendation for improvement would be to have the ContextMenu behave like a Popup. For example:
myContextMenu.IsOpen = True;
would cause the menu to be displayed, and
myContextMenu.IsOpen = False;
would cause it to be hidden.
In this way, the behavior could alternatively be to display/hide without the ContextMenu capturing any events. This would also allow any ContextMenu to be displayed via a single event based on the current state of the application.
One recommendation for improvement would be to have the ContextMenu behave like a Popup. For example:
myContextMenu.IsOpen = True;
would cause the menu to be displayed, and
myContextMenu.IsOpen = False;
would cause it to be hidden.
In this way, the behavior could alternatively be to display/hide without the ContextMenu capturing any events. This would also allow any ContextMenu to be displayed via a single event based on the current state of the application.
9 Answers, 1 is accepted
0
Hello Gary,
What you can do in your scenario is handle the event for which the context menu is listening in the cases when you don't want it to be displayed, i.e. you can do the following:
Kind regards,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
What you can do in your scenario is handle the event for which the context menu is listening in the cases when you don't want it to be displayed, i.e. you can do the following:
<
nav:RadTreeView x:Name="treeView" Selected="treeView_Selected">
<nav:RadContextMenu.ContextMenu>
<nav:RadContextMenu x:Name="contextMenu" EventName="Selected">
That way you can implement the logic which determines whether the context menu should be displayed in the in the treeView_Selected method. I am attaching a sample project which demonstrates what I am suggesting. Please let me know if this solves your issue.
Kind regards,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
GEB
Top achievements
Rank 1
answered on 12 Jan 2009, 04:04 PM
Thank you for your quick response. howeverr, I have a follow up question. I am able to capture the event, just as you do in your sample. However, when I capture the event, what method do I use to cause the context menu to be displayed? Note that the control that is generating the event is another third-party event, as opposed to a RadTreeView object.
In your sample, you have:
In your sample, you have:
RadTreeView tree = sender
as RadTreeView;
if ((e.OriginalSource as RadTreeViewItem).Header.ToString() != "Item 2")
{
e.Handled =
true;
}
else
{
// What do I do here to get the ContectMenu to pop up? It is NOT being displayed automatically.
}
Within this method, what calls do I make to show the contect menu?
0
Hi Gary,
It is strange that the context menu doesn't show up when you run the example I sent you - it does on my machine. My guess would be that the third-party control you use does not fire a Selected event and you should specify an event that is fired by the control you use. Anyways, if the context menu doesn't show up automatically you can set its IsOpen property to true like so:
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
It is strange that the context menu doesn't show up when you run the example I sent you - it does on my machine. My guess would be that the third-party control you use does not fire a Selected event and you should specify an event that is fired by the control you use. Anyways, if the context menu doesn't show up automatically you can set its IsOpen property to true like so:
contextMenu
.IsOpen = true;
Hope that helps.
Kind regards,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
GEB
Top achievements
Rank 1
answered on 12 Jan 2009, 04:21 PM
Actually, it is even stranger. I can see the event fired from the 3rd party control. But, when I try to access the ContextMenu programatically in the code-behind event handler by the name of the ContextMenu, it compiles fine, but generates a run-time error, indicating that the ContextMenu is NULL. For example, borderContextMenu.IsOpen = true; generates a runtime error indicating that borderContextMenu is NULL, and that I should use the "new" directive.
0
Hello Gary,
The problem must be in the third-party control you use. Here is some theory: in order for the x:Name of a control to work, that control should be placed in the ContentProperty of the control it's placed in. Currently in Silverlight if you set the ContentProperty of a custom control this wouldn't work due to a bug in the plugin. So, if the third-party control you use is not based on say ContentControl or ItemsControl, this may be why the context menu is null - it cannot be found by its x:Name. It may sound a bit complicated but that would be my guess. If you were using one of our controls I could investigate further, but there is nothing I can do with a third-party control.
Greetings,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
The problem must be in the third-party control you use. Here is some theory: in order for the x:Name of a control to work, that control should be placed in the ContentProperty of the control it's placed in. Currently in Silverlight if you set the ContentProperty of a custom control this wouldn't work due to a bug in the plugin. So, if the third-party control you use is not based on say ContentControl or ItemsControl, this may be why the context menu is null - it cannot be found by its x:Name. It may sound a bit complicated but that would be my guess. If you were using one of our controls I could investigate further, but there is nothing I can do with a third-party control.
Greetings,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
GEB
Top achievements
Rank 1
answered on 13 Jan 2009, 03:18 PM
It appears you were absolutely correct about the ContentControl and ItemsControl. The vendor of the 3rd party control has indicated that there is a template available that could be used. It is in the form of:
<mywidget:AboutTemplate Canvase.Left="10" Canvas.Top="10">
</mywidget:AboutTemplate>
I attempted to insert a simple ContextMenu within the template, but it still does not display. Note that there is no event to trigger off of. Any recommendations on how to get the ContextMenu to work in a Template environment such as this?
<mywidget:AboutTemplate Canvase.Left="10" Canvas.Top="10">
</mywidget:AboutTemplate>
I attempted to insert a simple ContextMenu within the template, but it still does not display. Note that there is no event to trigger off of. Any recommendations on how to get the ContextMenu to work in a Template environment such as this?
0
Hello Gary,
Control templates use a different name scope from that of the application, so you won't be able to get the context menu from the template and tell it what to do. It is possible to get elements from the control template in WPF but unfortunatelly this is not available in Silverlight.
Regards,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Control templates use a different name scope from that of the application, so you won't be able to get the context menu from the template and tell it what to do. It is possible to get elements from the control template in WPF but unfortunatelly this is not available in Silverlight.
Regards,
Tihomir Petkov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ez
Top achievements
Rank 1
answered on 19 Mar 2009, 12:38 PM
Tihomir ,
I also need the context menu to only appear in certain circumstances.
I have followed your instructions laid out in your sample project.
The context menu is displaying despite my use of the e.Handled command.
I think the issue is, that the event I'm listening to in the Control in which the context menu is placed is the MouseLeftButtonUp event. Therefor, the context menu's displaying (or not) is not dependant on how the parent control's event is handled.
In your example the context menu is listening to a unique event that is fired in the Parent Control. (i.e. Selected)
Thanks for your help in advance,
ez
I also need the context menu to only appear in certain circumstances.
I have followed your instructions laid out in your sample project.
The context menu is displaying despite my use of the e.Handled command.
I think the issue is, that the event I'm listening to in the Control in which the context menu is placed is the MouseLeftButtonUp event. Therefor, the context menu's displaying (or not) is not dependant on how the parent control's event is handled.
In your example the context menu is listening to a unique event that is fired in the Parent Control. (i.e. Selected)
Thanks for your help in advance,
ez
0
Hello Ez,
Can you please elaborate a bit more on the exact scenario you want to implement?
Best wishes,
Tihomir Petkov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Can you please elaborate a bit more on the exact scenario you want to implement?
Best wishes,
Tihomir Petkov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.