2

I want to enable text wrapping in the WPF DataGrid column headers and the content of the rows. Searching for solutions, I often stumble over something like this. The problem is, that it is not working for me.

First of all I have problems with this line:

xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"

I get errors about the assembly not being found. More problems with the rest of the XAML-code.

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

I place this inside the DataGrid tag, otherwise it won't compile. I also omit the "primitives"-namespace as I did not actually include it (see above). Now it compiles. However the application throws some exception in the constructor of the window. Any idea how I can get this thing to actually work?

Community
  • 1
  • 1
B_old
  • 1,141
  • 3
  • 12
  • 26

2 Answers2

8

Please see this first Text wrapping in WPF DataGrid column header

Amit
  • 21,570
  • 27
  • 74
  • 94
  • Thanks for the answer. I took a look at this. I have to omit the x:Key attribute because of compile errors. I then only placed the style-tag inside of the DataGrid-tag and it compiled. I still get the same crash though. Also, I am creating my columns dynamically in c#-code. Does the proposed method work in that case as well? – B_old May 27 '11 at 09:11
4

The reference to app.xaml is not required as can be seen here:

<DataGrid Name="WBdataGrid" AutoGenerateColumns="False" ColumnHeaderHeight="50" >
  <DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
      <Setter Property="ContentTemplate">
        <Setter.Value>
          <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
McDowell
  • 107,573
  • 31
  • 204
  • 267
Nate
  • 751
  • 1
  • 7
  • 9