Y
Y
youngpr2020-10-04 13:14:31
WPF
youngpr, 2020-10-04 13:14:31

How to make a word wrap if it does not fit into the DataGrid?

When I enter text into the DataGrid, it endlessly goes horizontally without wrapping the text, in a word it knows no boundaries, how to fix this?

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <DataGrid x:Name="phonesList"  ItemsSource="{Binding}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="ФИО"  Binding="{Binding Name}" Width="160" />
                <DataGridTextColumn Header="Должность" Binding="{Binding Position}" Width="160"/>
                <DataGridTextColumn Header="Телефон" Binding="{Binding PhoneNumber}" Width="160"/>
                <DataGridTextColumn Header="Почта" Binding="{Binding Email}" Width="160"/>
                <DataGridTextColumn Header="Организация" Binding="{Binding Organisation}" Width="160"/>
            </DataGrid.Columns>

        </DataGrid>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Content="Добавить" Margin="10" Click="Add_Click" />
            <Button Content="Изменить" Margin="10" Click="Edit_Click"  />
            <Button Content="Удалить" Margin="10" Click="Delete_Click"  />
        </StackPanel>
    </Grid>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2020-10-05
@Sumor

Code from Stackoverflow:

<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>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question