KellyM
KellyM

Reputation: 2522

C#/WPF Allowing Line Breaks in Dynamically Generated Datagrid

I am building an application that uses a DataGrid. One column allows a user to input comments, and another contains description of the item. The problem is that I want the Comments section to comprise the bulk of the row, but the Description column is taking up a lot of horizontal space. There is ample vertical room, but it only displays in a single line. Is there a way to force it to break into multiple lines? Limiting the width just cuts off the data.

My XAML

<Window x:Class="hotels.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="1000" Loaded="Window_Loaded"  WindowStyle="ThreeDBorderWindow">
    <Window.Resources>
        <Style x:Key="Grouping" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBlock Margin="10px" Foreground="White" Text="{Binding Name}" FontFamily="Arial Black" FontSize="18" Background="Blue">
                            </TextBlock>
                            <ItemsPresenter>


                            </ItemsPresenter>

                        </StackPanel>
                    </ControlTemplate> 
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <ScrollViewer>
    <StackPanel  Orientation="Vertical" Margin="20">

        <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10, 0, 30, 0" VerticalAlignment="Center" >
            <Label Content="Room:" HorizontalAlignment="Left" Margin="0,0,0,0"  VerticalAlignment="Center" />
            <TextBox x:Name="roomTextBox"  Margin="5,0,0,0" TextWrapping="Wrap"  Width="50" Panel.ZIndex="-1" VerticalAlignment="Center"/>
            <Label x:Name="locationLabel" Content="Location:" Margin="25,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Right"/>
            <ComboBox x:Name="locationComboBox"  SelectionChanged="filterEmployees" ItemsSource="{Binding}" Margin="5,0,0,0" SelectedIndex="-1" VerticalAlignment="Center" Width="150"/>




            <Label x:Name="inspectLabel" Content="Inspector:" HorizontalAlignment="Left" Margin="50,0,0,0" VerticalAlignment="Center" Height="27"/>
            <ComboBox x:Name="inspectorBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="15,0,0,0" VerticalAlignment="Center" Width="100"/>
            <Label x:Name="empLabel" Content="Attendant:" HorizontalAlignment="Left" Margin="50,0,0,0" VerticalAlignment="Center"/>
        <ComboBox x:Name="employeeBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Width="100"/>



        </WrapPanel>
            <WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="50, 0, 50, 0">
                <Label x:Name="scoreLabel" Content="Score: " FontFamily="Arial Black" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
                <Label x:Name="currentPointLabel" FontSize="24" Foreground="IndianRed" FontFamily="Arial Black" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
                <Label  FontSize="24" Foreground="ForestGreen" FontFamily="Arial Black" x:Name="totalPointLabel" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
            </WrapPanel>

            <DataGrid CanUserAddRows="False" CanUserDeleteRows="False" x:Name="itemGrid" Width="Auto"  AutoGenerateColumns="False"  ItemsSource="{Binding}"  Margin="0,0,0,0"  MinHeight="400" AlternatingRowBackground="LightSteelBlue" RowBackground="SteelBlue" CanUserResizeRows="False" CanUserSortColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" FontFamily="Arial Black" VerticalContentAlignment="Center" VerticalAlignment="Center"  >
                <DataGrid.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource Grouping}">
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <DataGridRowsPresenter></DataGridRowsPresenter>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </DataGrid.GroupStyle>

            <DataGrid.Columns>

                    <DataGridTextColumn IsReadOnly="True" Width="auto"   Header="Description" Binding="{Binding Description}" CanUserResize="False" />
                <DataGridTextColumn IsReadOnly="True"  Width="auto" Header="Points Possible" Binding="{Binding Points}" />

                <DataGridTemplateColumn Header="Deductions" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                                <ComboBox   Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" Width="Auto" ItemsSource="{Binding Score}" SelectedIndex="0" SelectionChanged="updateScore" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox  ItemsSource="{Binding Score}" SelectedIndex="0" SelectionChanged="updateScore" Height="40" Width="Auto" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn IsReadOnly="True"   Width="50" Header="Score" Binding="{Binding Current}"  />
                    <DataGridTemplateColumn Header="Comments" MinWidth="100" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>

                            <TextBox Text="{Binding Comments}" Margin="10" Width="Auto" Height="100" TextChanged="TextBox_TextChanged"></TextBox>

                            </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Comments, Mode=TwoWay}" Margin="10" Width="Auto" Height="100"></TextBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>


            </DataGrid.Columns>

        </DataGrid>

            <Button x:Name="submitButton" Click="submitData" Content="Submit" HorizontalAlignment="right" Margin="00, 0, 00, 00 " VerticalAlignment="Center" Width="75"/>



    </StackPanel>
    </ScrollViewer>
</Window>

and a screenshot so perhaps y'all can get a better idea of what I am meaning

enter image description here

Thanks!

Upvotes: 0

Views: 1123

Answers (2)

PlantPorridge
PlantPorridge

Reputation: 185

You need add an ElementStyle to your DataGridTextColumn, and use it to set the TextWrapping Property:

<DataGridTextColumn IsReadOnly="True" Width="auto" Header="Description" Binding="{Binding Description}" CanUserResize="False" />
      <DataGridTextColumn.ElementStyle>
           <Style>                            
               <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
           </Style>
      </DataGridTextColumn.ElementStyle>
<DataGridTextColumn/>

Upvotes: 1

Sean
Sean

Reputation: 346

You need to add TextWrapping="Wrap" to the comments textboxes.

Upvotes: 0

Related Questions