【问题标题】:C#/WPF Allowing Line Breaks in Dynamically Generated DatagridC#/WPF 允许在动态生成的数据网格中换行
【发布时间】:2016-05-19 19:01:43
【问题描述】:

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

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

还有一张截图,也许你们可以更好地理解我的意思

谢谢!

【问题讨论】:

    标签: wpf xaml datagrid


    【解决方案1】:

    您需要在 DataGridTextColumn 中添加一个 ElementStyle,并使用它来设置 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/>
    

    【讨论】:

      【解决方案2】:

      您需要将 TextWrapping="Wrap" 添加到 cmets 文本框。

      【讨论】:

      • 谢谢,但这不是我需要包装的评论框 - 它是描述列中的文本。对不起,如果我有点不清楚。
      猜你喜欢
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2017-05-12
      相关资源
      最近更新 更多