2011-05-09 116 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