2
votes

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>
1
Picture 2 has a fixed width that happens to hold all the text in the cell. If the width was smaller the text would not wrap. Just some clarification.GregN
“Path gets confused”, “after I place the datagrid”, “invalidates the binding” — can you clarify what’s meant by those phrases?15ee8f99-57ff-4f92-890c-b56153
I originally started with Picture 2. It takes that collection and binds the elements: a name string and a message string. It works but the message string is too long for the width and I want to wrap the message. So that's when I added the custom cellstyle to apply the text wrapping (like the column header). After applying the cell style the data in the binding is the name of the collection instead of the properties of the collection (Picture 1).GregN
So you’re doing something somewhere with these styles. Please show the XAML. Do I not need to worry about “place the datagrid”?15ee8f99-57ff-4f92-890c-b56153
Added the xaml of the custom dg.GregN

1 Answers

4
votes

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.