locked
DataGrid sizing column to fit RRS feed

  • Question

  • Hi all, say I have a DataGrid with 2 columns, the first containing a small value, the second containing a text with several sentences. The DataGrid is included into a Grid in a custom control, and the 2nd column is a DataGridTemplateColumn so that I can let the long text wrap. Here is a sample XAML:

    <DataGrid AutoGenerateColumns="False" ... >
     <DataGrid.Columns>
      ... other column ...
    
      <DataGridTemplateColumn ...>
       <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
         <TextBox TextWrapping="Wrap"
            Text="..."
            MinWidth="400" MaxWidth="700"
            MaxHeight="400"
            ScrollViewer.VerticalScrollBarVisibility="Auto"/>
        </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
     </DataGrid.Columns>
    </DataGrid>
    
    

    Now, I set max height to a reasonable amount, so that the row does not grow too much, and I did the same with width; but I'd rather like it to grow to fit all the free space left for the datagrid by its container. What would be the best approach for automatically sizing the width of this last column to fill all the available space, letting the rest of the content wrap and scrollbars appear when required, thus removing the maxwidth constraint? Thanx!

    Saturday, March 12, 2011 10:11 AM

Answers

  • That behavior is not impelmented. You'll have listen to some Loaded event (e.g., the TextBox in the DataTemplate), do the maths and set column's width "manually".

     

    Update: Maybe, DataGrid.SizeChanged is not a bad idea.


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    • Edited by Bigsby Sunday, March 13, 2011 12:48 AM update
    • Marked as answer by Jie Bao Sunday, March 20, 2011 6:07 PM
    Sunday, March 13, 2011 12:47 AM
  • Hi Naftis,

    I think below code is what you want to do:

            <DataGridTemplateColumn Header="Text" Width="*">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <ScrollViewer>
                    <TextBox TextWrapping="Wrap" MaxHeight="400"
                         Text="..."                
                         ScrollViewer.VerticalScrollBarVisibility="Auto"/>
                  </ScrollViewer>
                </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    

    We can put the TextBox in one ScrollViewer, meanwhile, set the DataGridTemplateColumn.Width to "*" star value, that can use the left space of the DataGrid on the column width.

    I might have not understood your question, please feel free to let me know.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jie Bao Sunday, March 20, 2011 6:07 PM
    Monday, March 14, 2011 5:43 AM

All replies

  • That behavior is not impelmented. You'll have listen to some Loaded event (e.g., the TextBox in the DataTemplate), do the maths and set column's width "manually".

     

    Update: Maybe, DataGrid.SizeChanged is not a bad idea.


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    • Edited by Bigsby Sunday, March 13, 2011 12:48 AM update
    • Marked as answer by Jie Bao Sunday, March 20, 2011 6:07 PM
    Sunday, March 13, 2011 12:47 AM
  • Hi Naftis,

    I think below code is what you want to do:

            <DataGridTemplateColumn Header="Text" Width="*">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <ScrollViewer>
                    <TextBox TextWrapping="Wrap" MaxHeight="400"
                         Text="..."                
                         ScrollViewer.VerticalScrollBarVisibility="Auto"/>
                  </ScrollViewer>
                </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    

    We can put the TextBox in one ScrollViewer, meanwhile, set the DataGridTemplateColumn.Width to "*" star value, that can use the left space of the DataGrid on the column width.

    I might have not understood your question, please feel free to let me know.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jie Bao Sunday, March 20, 2011 6:07 PM
    Monday, March 14, 2011 5:43 AM