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
所以基本上我沒有告訴我的新寬度和高度的方式。
作爲提示,您可以使用編輯器中的101010按鈕將塊格式化爲代碼。 – itowlson
'Grid'是否佔用整個'Canvas'?你能從'Grid'獲得可靠的大小嗎? – Jay
初始網格不佔用整個Canvas,但是生成的網格可能不佔用整個Canvas。 有趣的是,如何將Canvas綁定到網格維度?目前,Canvas擴展並與窗口進行契約。 –