WPF Datagrid Cell Text Wrapping

datagridresourcedictionarywpfxaml

I have a datagrid that has a custom style so I can reuse this format throughout my application. It has a custom column headerstyle, row style, etc. I was able to get the text wrapping to work on the column header and data binds to it correctly. When I tried to use the same technique on the cell the binding does not appear to be working but the wrap does. I have read the following posts but it looks like I'm required to set the style each time after I place the datagrid. Can it not be done in a resource dictionary or am I applying the wrapping in the wrong spot?

wpf DataGrid change wrapping on cells

WPF toolkit datagrid cell text wrapping

WPF DataGrid Cell Text Wrapping – set to NoWrap (False)

Here is the datagrid definition (trimmed):

<Style x:Key="EmbedDG" TargetType="{x:Type DataGrid}" >
    <Setter Property="ColumnHeaderStyle" Value="{DynamicResource DGCH}" />
    <Setter Property="CellStyle" Value="{DynamicResource EmbedDGCE}" />
</Style>    

Here is the working DGCH style showing the text wrapping:

<Style x:Key="DGCH" TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here is the cellstyle that does not work (Picture 1 with contenttemplate, 2 without):

<Style x:Key="EmbedDGCE" TargetType="{x:Type DataGridCell}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Screenshot with the CellStyle ContentTemplate Applied

Screenshot without the CellStyle ContentTemplate Applied

EDIT:

<DataGrid Style="{DynamicResource EmbedDG}" ItemsSource="{Binding Tags}" >
      <DataGrid.Columns>
          <DataGridTextColumn Header="Tag Type" Binding="{Binding TagType}" Width="180" />
          <DataGridTextColumn Header="Tag Comments" Binding="{Binding Message}" Width="300"/>
      </DataGrid.Columns>
</DataGrid>

Best Answer

I'd get rid of the cell style and use template columns.

<DataGrid Style="{DynamicResource EmbedDG}" ItemsSource="{Binding Tags}" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Tag Type" Width="180">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock 
                        Text="{Binding TagType}" 
                        TextWrapping="Wrap"
                        />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Tag Comments" Width="300">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock 
                        Text="{Binding Message}" 
                        TextWrapping="Wrap"
                        />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

I suppose it's giving you the fully qualified class name because your cell template is trying to display an object in a TextBlock. I don't have time to play with it, but whatever the issue is in your code, the above should work.

Related Topic