【问题标题】:How to limit Resize Datagrid based on Window resize?如何根据窗口调整大小限制调整数据网格的大小?
【发布时间】:2011-05-09 21:40:27
【问题描述】:

我想知道我使用的代码有什么问题。我试图通过下面提供的代码来限制基于浏览器窗口的数据网格的高度调整大小。也许,有更好的方法来做到这一点。非常感谢任何建议。

当我调整窗口大小超过我得到错误。

*System.ArgumentException 未被用户代码处理 Message=Value 不在预期范围内。 堆栈跟踪: 在 MS.Internal.XcpImports.CheckHResult(UInt32 小时) 在 MS.Internal.XcpImports.SetValue(IManagedPeerBase obj,DependencyProperty 属性,Double d) 在 System.Windows.DependencyObject.SetValue(DependencyProperty 属性,双 d) 在 System.Windows.FrameworkElement.set_Height(双值) 在 SilverlightResizeTest.Content_Resized(Object sender, EventArgs e) 在 System.Windows.Interop.Content.FireResized(对象发送者,EventArgs args) 在 System.Windows.Interop.SilverlightHost.FireResized(对象发送者,EventArgs args) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex, 字符串事件名称) 内部异常:*

代码:

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);
        }
    }

【问题讨论】:

    标签: silverlight datagrid


    【解决方案1】:

    以下是我认为出错的地方:如果应用程序的内容小于 485,您的身高将变为负数。我很确定负高度会超出预期值的范围。

    我不确定您是否尝试通过在 xaml 中使用正确的控件来完成此操作。具有两行且其中一个行高定义为 485 的简单网格布局将实现相同的效果。

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

    【讨论】:

    • 我需要能够避免我包含的代码中出现负值。您的方式适用于不同的任务。再次感谢您。
    • 在这种情况下,只需在减去之前检查 x >= 485。根据该答案,您可以减去 485 或将 x 设置为 0。
    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2014-12-28
    • 2017-08-04
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多