2011-05-09 187 views
0

我想知道我使用的代碼有什麼問題。我想通過下面提供的代碼來限制基於瀏覽器窗口的數據網格的高度調整大小。也許,有一個更好的方法來做到這一點。任何建議是高度讚賞。如何根據窗口大小限制調整大小的Datagrid?

當我調整窗口的大小比我收到錯誤太多。

* System.ArgumentException由用戶代碼未處理 消息=值不在預期範圍內。 堆棧跟蹤: 在MS.Internal.XcpImports.CheckHResult(UInt32的小時) 在MS.Internal.XcpImports.SetValue(IManagedPeerBase OBJ,的DependencyProperty屬性,雙人d) 在System.Windows.DependencyObject.SetValue(的DependencyProperty屬性,雙人d ) at System.Windows.FrameworkElement.set_Height(Double value) at SilverlightResizeTest.Content_Resized(Object sender,EventArgs e) at System.Windows.Interop.Content.FireResized(Object sender,EventArgs args) at System.Windows。 Interop.SilverlightHost.FireResized(Object sender,EventArgs args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex, String eve ntName) 的InnerException:*

代碼:

public SilverlightResizeTest() 
    { 
     InitializeComponent(); 
     // Set the height for the DataGrid when the browser window changes size 
     App.Current.Host.Content.Resized += new System.EventHandler(Content_Resized); 

     // Set the initial height for the DataGrid 
     double x = App.Current.Host.Content.ActualHeight; 
     if (x != 0) 
     { 
      DataGrid.Height = (x - 485.0); 
     } 
    } 

    void Content_Resized(object sender, System.EventArgs e) 
    { 
     // Set the height for the DataGrid when the browser window changes size 
     double x = App.Current.Host.Content.ActualHeight; 
     if (x != 0) 
     { 
      DataGrid.Height = (x - 485.0); 
     } 
    } 

回答

0

這就是我認爲是,雖然走錯了:如果應用程序的內容變得比485越小,你的身高變成了負數。我敢肯定,負面高度會超出預期值的範圍。

我不確定你想要做什麼,不能通過在xaml中使用正確的控件來完成。具有兩行的簡單網格佈局以及在485處定義的行高度之一將實現相同。

<Grid> 
    <Grid.RowDefinitions> 
    <RowDefiniton Height="485"/> 
    <RowDefiniton Height="*"/> 
    </Grid.RowDefinitions> 
    ... 
</Grid> 
+0

我需要能夠避免從我包括的代碼的負值。你的方式適合完成不同的任務。再次感謝你。 – vladc77 2011-05-10 16:17:49

+0

在這種情況下,只需檢查x> = 485,然後再減去。基於這個答案,你可以減去485或將x設置爲0。 – Danexxtone 2011-06-02 15:48:11

相關問題