C#/ WPF允许动态生成的Datagrid中的换行符

时间:2016-05-19 19:01:43

标签: wpf xaml datagrid

我正在构建一个使用DataGrid的应用程序。一列允许用户输入注释,另一列包含项目的描述。问题是我希望“注释”部分构成行的大部分,但“描述”列占用了大量的水平空间。有充足的垂直空间,但它只显示在一行。有没有办法强迫它分成多行?限制宽度只会切断数据。

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

和屏幕截图所以也许你们都可以更好地了解我的意思

enter image description here

谢谢!

2 个答案:

答案 0 :(得分:1)

您需要将ElementStyle添加到DataGridTextColumn,并使用它来设置TextWrapping属性:

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

答案 1 :(得分:0)

您需要将TextWrapping =“Wrap”添加到评论文本框中。