How to display text in a WPF GridView that contain

2019-07-20 04:25发布

问题:

I have a grid view that has two simple columns, a report number and a short description. It is common for this short description to have some carriage returns. If that is the case, I would like the row to expand its height to fit all of the text in.

Currently the field renders with possibly the first line displaying, but then cutting off text below it. How could I display all the text, with it adjusting the height as needed?

And can this be done on a per-row basis, as in, if the next report did not have a description with carriage returns, it would just show on one line?

It would be rather similar to this question, but in WPF versus ASP.net

This also may due to using a theme application wide. If that is the case, how can I enforce it only in this grid view?

XAML:

Originally I had it only as

<GridViewColumn Width="365" Header="Desc" DisplayMemberBinding="{Binding desc}" />

I have also tried

<GridViewColumn x:Name="colDesc" Header="Desc" Width="75" >
     <GridViewColumn.CellTemplate>
          <DataTemplate>
              <TextBlock Text="{Binding Desc}" />
          </DataTemplate>
     </GridViewColumn.CellTemplate>
</GridViewColumn>

We are using an application wide theme, that very well may be setting the high explicitly. If so, I am leaning mostly towards the fact (From the comments) that the theme is overriding a default value for how this works. If so, how do I have it act the way the it is supposed to (again, from the comments, it sounds like this is the default behavior).

Using the second example, here is what it looks like:

In here it looks like the text is larger, so again, I would want the cell to adjust to the hight needed. And some of these have multiple lines, so as you can see I would not see the remaining content. I have also tried to set the Height on the text block, but that had no effect.

回答1:

This worked.

 public string TextBlockText { get { return "one line \r\ntwo line"; } }

 <TextBlock Height="Auto" Width="100" HorizontalAlignment="Left" Text="{Binding Path=TextBlockText}" />

I will leave it to you on how to put a TextBlock in a ListView.