2015-04-22 20 views
-1

我有一個模板化的Silverlight控件項目。當我從項目我添加的MainPage的DLL(與模板化控件),並希望用右鍵點擊打開上下文菜單,我得到這個錯誤:Silverlight上下文菜單IsOpen異常?

MainPage.xaml中

<MyControls:Draw x:Name="ctrDraw"></MyControls:Draw> 

Draw.cs(模板化的Silverlight控制)

_contextMenu.IsOpen = true; --> Error 

的ErrorMessage

errMsg "Unhandled Error in Silverlight Application 
Code: 4004  
Category: ManagedRuntimeError  
Message: Das Festlegen von Eigenschaft 'System.Windows.FrameworkElement.Style' hat eine Ausnahme ausgelöst." 
+0

你可以提供更多的源代碼嗎?我試圖確定您是否有_contextMenu正確定義 – Pseudonym

+0

請參閱我的答案中的代碼。 – fellowes22

回答

1

初始化:

private ContextMenu _contextMenu; 
private MenuItem _contextMenuItem; 

事件:

private void map_MouseRightButtonUp(object sender, GraphicMouseButtonEventArgs e) 
{ 
_contextMenu = new ContextMenu(); 

_contextMenuItem = new MenuItem(); 
_contextMenuItem.Header = "Edit"; 
_contextMenu.Items.Add(_contextMenuItem); 
_contextMenuItem.Click += new RoutedEventHandler(menuItem_Click); 

.....

0

我不記得確切,但你可以找到 「ContextMenuService」 級和嘗試設置如:

ContextMenuService.SetContextMenu(ctrDraw, _contextMenu) 
+0

如果我直接在主頁面文件中創建ContextMenu,則方法IsOpen可以工作。當我添加一個DLL(我自己的控制)到我的項目,這包括一個上下文菜單它崩潰。 – fellowes22