Reputation: 11
I have searched through many times but have not seen this before. Probably really simple question but can't wrap my head around it.
Wrote a VSTO add-in for Excel that draws a Grid dynamically. Then launches a new window and replaces the contents of the Canvas with the generated Grid. The problem is with printing. When I call the print procedure the canvas.height and canvas.width returned is the old value prior to replacing it with the grid.
Sample:
string="<Grid Name=\"CanvasGrid\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">..Lots of stuff..</Grid>";
// Launch new window and replace the Canvas element
WpfUserControl newWindow = new WpfUserControl();
newWindow.Show();
//To test
MessageBox.Show(myCanvas.ActualWidth.ToString());
//return 894
Grid testGrid = myCanvas.FindName("CanvasGrid") as Grid;
MessageBox.Show("Grid " + testGrid.ActualWidth.ToString());
//return 234
StringReader stringReader = new StringReader(LssAllcChrt);
XmlReader xmlReader = XmlReader.Create(stringReader);
Canvas myCanvas = newWindow.FindName("GrphCnvs") as Canvas;
myCanvas.Children.Clear();
myCanvas.Children.Add((UIElement)XamlReader.Load(xmlReader));
//To test
MessageBox.Show(myCanvas.ActualWidth.ToString());
//return 894 but should be much larger the Grid spans all three of my screens
Grid testGrid = myCanvas.FindName("CanvasGrid") as Grid;
MessageBox.Show("Grid " + testGrid.ActualWidth.ToString());
//return 234 but should be much larger the Grid spans all three of my screens
//Run code from WpfUserControl.cs after it loads from button click
Grid testGrid = canvas.FindName("CanvasGrid") as Grid;
MessageBox.Show("Grid " + testGrid.ActualWidth.ToString());
//return 234 but should be much larger the Grid spans all three of my screens
So basically I have no way of telling what my new width and height are.
Upvotes: 1
Views: 1739
Reputation: 11
Ok here is what I did to fix the issue.
Basically I was looking to print the grid I created if it went out of the visual bonds. I changed the new window to TabControl and TabItems because it was a new requirement (Yeah me (sarcasm)). So what I did was to reference the selected TabItem, reference the ScrollViewer as the only content in the tabitem and reference the Grid as a FrameworkElement:
TabItem ti = GBtabControl.SelectedItem as TabItem;
ScrollViewer sc = ti.Content as ScrollViewer;
FrameworkElement element = sc.Content as FrameworkElement;
The element gives the correct width and height and I can now print and export the chart as a png file.
Thanks for the support
Upvotes: 0