I know this sounds bizarre, but it is true.
I have a simply WPF application that hosts a Visio control. There are no problems with that. Some essential events, such as DocumentOpened does work.
But if I want to handle other events, for instance, BeforeShapeDeleted, CellChanged, they stop firing once I bind Shapes to a ListBox in the DocumentOpened.
Here is my code:
public partial class MainWindow : Window { private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl visioControl = new AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl(); public MainWindow() { InitializeComponent(); this.host.Child = this.visioControl; } private void Window_Loaded(object sender, RoutedEventArgs e) { this.visioControl.DocumentOpened += new AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEventHandler(visioControl_DocumentOpened); this.visioControl.Window.Application.BeforeShapeDelete += new Microsoft.Office.Interop.Visio.EApplication_BeforeShapeDeleteEventHandler(Application_BeforeShapeDelete); this.visioControl.Window.Application.CellChanged += new Microsoft.Office.Interop.Visio.EApplication_CellChangedEventHandler(Application_CellChanged); } void Application_CellChanged(Microsoft.Office.Interop.Visio.Cell Cell) { MessageBox.Show("Changed"); } void Application_BeforeShapeDelete(Microsoft.Office.Interop.Visio.Shape Shape) { MessageBox.Show("Deleted"); } void visioControl_DocumentOpened(object sender, AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEvent e) { //if I comment the line bellow BeforeShapeDelete and CellChanged will work, if I leave it uncommented, they won't work... lstShapes.ItemsSource = this.visioControl.Window.Application.ActivePage.Shapes; } private void mnuOpen_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlgOpenDiagram = new Microsoft.Win32.OpenFileDialog(); if (dlgOpenDiagram.ShowDialog() == true) { this.visioControl.Src = dlgOpenDiagram.FileName; } } }
The issue lies in the DocumentOpened method in the line that defines an ItemsSource...