WPF工具包datagrid单元格文本换行

时间:2022-01-01 22:20:02

My WPF datagrid's columns are fixed width, which means long text in the rows are cut off. How can I have the text wrap?

我的WPF datagrid的列是固定宽度,这意味着行中的长文本被截断。我怎么能有文字换行?

3 个解决方案

#1


8  

If you are using a DataGridTextColumn, you need to define the Style for the DataGridTextColumn.ElementStyle

如果您使用DataGridTextColumn,则需要为DataGridTextColumn.ElementStyle定义样式

<dg:DataGridTextColumn Header="SomeLongText" Binding="{Binding MyText}">
  <dg:DataGridTextColumn.ElementStyle>
    <Style TargetType="TextBlock">
      <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
  </dg:DataGridTextColumn.ElementStyle>
</dg:DataGridTextColumn>

Full explination can be found at the following http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn(v=vs.95).aspx

完整的探索可以在以下http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn(v=vs.95).aspx找到

#2


5  

You can replace the cell with a Textblock with Textwrapping enabled. i.e.

您可以使用启用了Textwrapping的Textblock替换单元格。即

<dg:DataGridTemplateColumn Header="Description" Width="*">
   <dg:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
           <TextBlock Text="{Binding Description}" TextWrapping="WrapWithOverflow"/>                                    
       </DataTemplate>
   </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

#3


1  

If your DataGridTextColumn is being created in the code behind you can set the style and setters this way:

如果在后面的代码中创建了DataGridTextColumn,则可以通过以下方式设置样式和设置器:

        _dataGridTextColumn.MaxWidth = 550;
        _dataGridTextColumn.ElementStyle = new System.Windows.Style(typeof(TextBlock));
        _dataGridTextColumn.ElementStyle.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));

This will cause the text inside the _dataGridTextColumn to wrap as it would inside a TextBlock.

这将导致_dataGridTextColumn中的文本像在TextBlock中一样进行换行。

#1


8  

If you are using a DataGridTextColumn, you need to define the Style for the DataGridTextColumn.ElementStyle

如果您使用DataGridTextColumn,则需要为DataGridTextColumn.ElementStyle定义样式

<dg:DataGridTextColumn Header="SomeLongText" Binding="{Binding MyText}">
  <dg:DataGridTextColumn.ElementStyle>
    <Style TargetType="TextBlock">
      <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
  </dg:DataGridTextColumn.ElementStyle>
</dg:DataGridTextColumn>

Full explination can be found at the following http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn(v=vs.95).aspx

完整的探索可以在以下http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn(v=vs.95).aspx找到

#2


5  

You can replace the cell with a Textblock with Textwrapping enabled. i.e.

您可以使用启用了Textwrapping的Textblock替换单元格。即

<dg:DataGridTemplateColumn Header="Description" Width="*">
   <dg:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
           <TextBlock Text="{Binding Description}" TextWrapping="WrapWithOverflow"/>                                    
       </DataTemplate>
   </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

#3


1  

If your DataGridTextColumn is being created in the code behind you can set the style and setters this way:

如果在后面的代码中创建了DataGridTextColumn,则可以通过以下方式设置样式和设置器:

        _dataGridTextColumn.MaxWidth = 550;
        _dataGridTextColumn.ElementStyle = new System.Windows.Style(typeof(TextBlock));
        _dataGridTextColumn.ElementStyle.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));

This will cause the text inside the _dataGridTextColumn to wrap as it would inside a TextBlock.

这将导致_dataGridTextColumn中的文本像在TextBlock中一样进行换行。