XAML为Canvas获得新的宽度和高度

时间:2010-03-12 00:25:49

标签: c# .net wpf xaml canvas

我已经多次搜索但之前没有见过这个。可能是非常简单的问题,但无法绕过它。

为Excel绘制了一个动态绘制网格的VSTO加载项。然后启动一个新窗口并用生成的Grid替换Canvas的内容。问题在于打印。当我调用print程序时,返回的canvas.height和canvas.width是用网格替换之前的旧值。

样品:

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

所以基本上我无法告诉我的新宽度和高度是什么。

1 个答案:

答案 0 :(得分:0)

好的,我这样做是为了解决这个问题。

基本上我打算打印我创建的网格,如果它脱离了视觉关系。我将新窗口更改为TabControl和TabItems,因为这是一个新要求(是的我(讽刺))。所以我做的是引用选定的TabItem,引用ScrollViewer作为tabitem中的唯一内容,并将Grid引用为FrameworkElement:

TabItem ti = GBtabControl.SelectedItem as TabItem;
ScrollViewer sc = ti.Content as ScrollViewer;
FrameworkElement element = sc.Content as FrameworkElement;

元素给出正确的宽度和高度,我现在可以打印并将图表导出为png文件。

感谢您的支持