XAML get new width and height for Canvas

Posted by Jack Navarro on Stack Overflow See other posts from Stack Overflow or by Jack Navarro
Published on 2010-03-12T00:25:48Z Indexed on 2010/03/24 22:43 UTC
Read the original article Hit count: 228

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf