How to limit Resize Datagrid based on Window resize?
I’d like to find out what is wrong with the code I use. I am trying to limit the height resize of the datagrid based on browser window by the code behind provided below. Perhaps, there is a better way of doing that. Any advice is highly appreciated.
When I resize the window too much than I am getting error.
*System.ArgumentException was unhandled by user code Message=Value does not fall within the expected range. StackTrace: at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, Double d) at System.Windows.DependencyObject.SetValue(DependencyProperty property, Double 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 eventName) InnerException:*
The code:
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);
}
}
Here's what I think is going wrong though: if the content of the app gets to be smaller than 485, your height becomes a negative number. I'm pretty sure that negative heights would fall out of the range of expected values.
I'm not sure if what you're trying to do, couldn't be accomplished by using the right controls in xaml though. A simple grid layout with two rows with one of the row heights defined at 485 would accomplish the same.
<Grid>
<Grid.RowDefinitions>
<RowDefiniton Height="485"/>
<RowDefiniton Height="*"/>
</Grid.RowDefinitions>
...
</Grid>
精彩评论