2

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>
GregN
  • 142
  • 1
  • 13
  • 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 Nov 15 '17 at 20:07
  • “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 Nov 15 '17 at 20:53
  • 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 Nov 15 '17 at 21:09
  • 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 Nov 15 '17 at 21:11
  • Added the xaml of the custom dg. – GregN Nov 15 '17 at 21:13
  • You should be using DataGridTemplateColumn. Same DataTemplate. Get rid of the cell style. – 15ee8f99-57ff-4f92-890c-b56153 Nov 15 '17 at 21:18

1 Answers1

4

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.

  • This is how I've seen it done on multiple forums but was hoping I could do some higher oridinal setting. I guess it's not a major deal but requires that anytime I need to wrap text for a column I've got to add additional boiler plate code for wrapping. Was hoping to just apply a style and be done with it. Do you happen to know any other way? – GregN Nov 15 '17 at 21:31
  • I'm on my way out of the office. – 15ee8f99-57ff-4f92-890c-b56153 Nov 15 '17 at 21:32
  • I couldn't find a better way so just went about your method. – GregN Nov 21 '17 at 16:36