Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

WPF: text wrapping inside DataGrid cells

When working with the DataGrid in WPF, you generally use the DataGridTextColumn object for displaying or editing text. Such an object has a limitation: there is no property for specifying that the text should be automatically wrapped according to the column width, similarly to what you can do in Microsoft Excel.

Fortunately the solution is pretty easy. In fact you can use the DataGridTemplateColumn element (a generic object which allows deep customizations for cell templates) and inside it you implement one TextBox for text editing and one TextBlock for displaying text; such controls expose the TextWrapping property that we can use for wrapping the text.

Here's the XAML code:

                <DataGridTemplateColumn x:Name="ColumnName" Header="Column Header Goes Here">
                            <TextBox Text="{Binding Path=DataBoundProperty, Mode=TwoWay}" TextWrapping="Wrap"/>
                            <TextBlock Text="{Binding Path=DataBoundProperty, Mode=OneWay}" TextWrapping="Wrap"/>

By assigning TextWrapping with Wrap the text will be automatically wrapped in case the column width is too small. In both cases I used an identifier named DataBoundProperty which must be replaced with the desired property from the data-bound object.


Print | posted on giovedì 14 ottobre 2010 20:08 | Filed Under [ Visual Studio 2010 Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 2 and 1 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET