Search code examples
wpfwpfdatagridapp.xaml

wpf DataGrid change wrapping on cells


I have a WPF DataGrid with AutoGenerate Columns. I have been able to override the column headers using code and also force wrapping on the column headers when I shrink the columns. When I try to force text wrapping on the cells my binding breaks... it shows the same value in every column.

Here is the XAML I am using to format

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell">
       <Setter Property="ContentTemplate">
           <Setter.Value>
               <DataTemplate>
                   <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
               </DataTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
   <Style TargetType="DataGridColumnHeader">
       <Setter Property="ContentTemplate">
           <Setter.Value>
               <DataTemplate>
                   <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
               </DataTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</DataGrid.ColumnHeaderStyle>

Again, the ColumnHeaderStyle works fine, but the CellStyle is not working.

Suggestions?

Update:

Column headers are set as follows:

if (e.Column.Header.ToString() == "Product_Description")
    e.Column.Header = "Product";

if (e.Column.Header.ToString() == "Original_Gross_Weight")
    e.Column.Header = "Orig. Net Wt.";

The wrapping of the headers works well. Just the wrapping of the content does not work.


Solution

  • On the binding it seems that once one replaces the DataGridCell data style, the full object for for the row is placed into the content presenter instead of the current property of the column.


    It appears you override AutoGeneratingColumn so why not simply turn of auto generate and define the columns by hand?

    Here is a working version where the text is wrapped for the data:

    <Window.Resources>
        <model:People x:Key="People">
            <model:Person First="Joe"   Last="Smith"   Phone="303-555 5555" />
            <model:Person First="Mary"  Last="Johnson" Phone="720-555 5555" />
            <model:Person First="Frank" Last="Wright"  Phone="202-555 5555" />
        </model:People>
    </Window.Resources>
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{StaticResource People}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First" Binding="{Binding First}" />
            <DataGridTextColumn Header="The Name" Binding="{Binding Last}" />
            <DataGridTextColumn Header="Phone Number" Binding="{Binding Phone}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="TextWrapping"
                                Value="Wrap" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
    
            </DataGridTextColumn>
        </DataGrid.Columns>
    
    </DataGrid>
    

    Result

    enter image description here