2010-03-12 28 views
1

我已經搜索了很多次,但之前沒有看到過。可能真的很簡單的問題,但不能包圍我的頭。XAML爲Canvas獲取新的寬度和高度

寫了一個Excel的VSTO加載項,動態地繪製網格。然後啓動一個新窗口並用生成的網格替換Canvas的內容。問題在於打印。當我調用打印過程時,返回的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 

所以基本上我沒有告訴我的新寬度和高度的方式。

+0

作爲提示,您可以使用編輯器中的101010按鈕將塊格式化爲代碼。 – itowlson

+0

'Grid'是否佔用整個'Canvas'?你能從'Grid'獲得可靠的大小嗎? – Jay

+0

初始網格不佔用整個Canvas,但是生成的網格可能不佔用整個Canvas。 有趣的是,如何將Canvas綁定到網格維度?目前,Canvas擴展並與窗口進行契約。 –

回答

0

好的,這是我做了什麼來解決這個問題。

基本上,我一直在尋找打印我創建的網格,如果它走出視覺債券。我將新窗口更改爲TabControl和TabItems,因爲這是一個新的要求(是我(諷刺))。所以我所做的就是引用所選的TabItem,引用的ScrollViewer作爲TabItem的唯一內容,並引用網格作爲一個FrameworkElement的:

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

元素給出正確的寬度和高度,現在我可以打印和將圖表導出爲png文件。

感謝您的支持