How get a WPF Datagrid with cells that wrap text instead of truncating it?

前端 未结 5 1122
你的背包
你的背包 2020-12-24 06:05

What must be done to get a WPF DataGrid with cells that wrap text instead of truncating it?

Right now when a text is bigger and don\'t fit in a column the text is tr

相关标签:
5条回答
  • 2020-12-24 06:28

    I made something similar to D.Rosados solution. Mine is however reusable if you have more columns that needs wrapping.

    <UserControl.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="WrapText">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
    </UserControl.Resources>
    
    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly="False" Header="Address" 
         Binding="{Binding Address}" ElementStyle="{StaticResource WrapText}" Width="150"/>
    </DataGrid.Columns>
    
    0 讨论(0)
  • 2020-12-24 06:28

    Another simple way of setting text wrap for Editing and Text DataGrid columns is to specity the Binding property and TextWrapping property as following:

    <DataGridTemplateColumn x:Name="ColumnName" Header="Column Header Goes Here">
            <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                             <TextBox Text="{Binding Path=DataBoundProperty, Mode=TwoWay}" TextWrapping="Wrap"/>
                    </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
            <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataBoundProperty, Mode=OneWay}" TextWrapping="Wrap"/>
                </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    
    0 讨论(0)
  • 2020-12-24 06:34

    "You could try to template the cells with a TextBlock which has text-wrapping enabled." If you use TextBlock, you will have trouble with copy/paste.

    0 讨论(0)
  • 2020-12-24 06:48

    You could try to template the cells with a TextBlock which has text-wrapping enabled.

    0 讨论(0)
  • 2020-12-24 06:50

    Thanks for your help @H.B., this did the trick for me (alignment is optional):

    <DataGrid.Columns>               
        <DataGridTextColumn Header="Wrapped & centered" Binding="{Binding field}">
            <DataGridTextColumn.ElementStyle>
                 <Style>                            
                     <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                     <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                 </Style>
             </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
    
    0 讨论(0)
提交回复
热议问题