Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
315
Need help specifying grid column behaviour
posted

Hi, I need help to produce the following behaviour with XamDatagrid's columns:

First things first, I'm only dealing with 2 columns

  • * users can resize the columns as they want.
  • * all the columns always fit exactly to the width of the control (no scrollbars or blank spaces).
  • * when the control's width is sized down, the last column retains its width, while the first shrinks in width.
  • * when the control's width is sized up, the first column retains its width, and the last column expands in width.

 So far, I've tried the xaml approach, using Autofit=True and using grids to specify the columns:

<ColumnDefinition Width="Auto" /> 

<ColumnDefinition Width="*" />

But the result was a grid where the columns couldn't be resized.

 Next I've tried doing it programmatically by listening to the sizechanged event, and trying to update the Field.Settings.Cell(Min|Max)Width property, but the changes were not reflected in grid.

Can anyone give me a simple standalone sample please?

Thanks!

** Here's the code that I'm currently trying to use **

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
    // Size difference
    double newSize =  sizeInfo.NewSize.Width - sizeInfo.PreviousSize.Width;

    int columnToEdit = 0;
    if (newSize<0)
    {
        // size shrinking. Hence, shrink the 1st column
        columnToEdit = 0;
    }
    else
    {
        // Size expanding. Hence, expand the 2nd column
        columnToEdit = 1;
    }
    
    // Calculate new width
    double newWidth = xdgField.DefaultFieldLayout.Fields[columnToEdit].CellWidthResolved + newSize;

    // Update width values (btw, I've tried different combinations here, eg, w/wo min/max, etc)
    xdgField.DefaultFieldLayout.Fields[columnToEdit].Settings.CellWidth = newWidth;
    xdgField.DefaultFieldLayout.Fields[columnToEdit].Settings.CellMinWidth = newWidth;
    xdgField.DefaultFieldLayout.Fields[columnToEdit].Settings.CellMaxWidth = newWidth;

    xdgField.DefaultFieldLayout.Fields[columnToEdit].Settings.LabelWidth = newWidth;
    xdgField.DefaultFieldLayout.Fields[columnToEdit].Settings.LabelMinWidth = newWidth;
    xdgField.DefaultFieldLayout.Fields[columnToEdit].Settings.LabelMaxWidth = newWidth;
    

    base.OnRenderSizeChanged(sizeInfo);
}