简体   繁体   中英

DataGridTextColumn - Displaying upto a maximum of three lines of text

I have a DataGridTextColumn ElementStyle as follows:

        <DataGridTextColumn.ElementStyle>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Setters>
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush>
                                <GradientStop Color="Black" Offset="0" />
                                <GradientStop Color="White" Offset="2" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="TextWrapping" Value="Wrap" />
                </Style.Setters>
            </Style>
        </DataGridTextColumn.ElementStyle>

I will need a cell in this column to display text in multiple lines but the number of lines should not exceed 3. The TextBlock does not have a MaxLines property so I cannot specify it directly. What's more the TextBlock does not have a Template property so I can't use a border less, read-only TextBox either. What can I do here?

Well I think the most simple way to achieve this is to use a TemplateColumn. Here is a quick sample : Second Column is a TextColumn where you can't do much, first column is a TemplateColumn with a TextBox that will be borderless and readonly, and MaxLines set to 3 :

<DataGrid>
    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Template Column" Width="30" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox IsReadOnly="True" BorderThickness="0" Text="{Binding Mode=OneWay}" MaxLines="3" TextWrapping="Wrap" Foreground="White">
                        <TextBox.Background>
                            <LinearGradientBrush>
                                <GradientStop Color="Black" Offset="0" />
                                <GradientStop Color="White" Offset="2" />
                            </LinearGradientBrush>
                        </TextBox.Background>
                    </TextBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTextColumn Header="Text Column" Binding="{Binding}" Width="30" >
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Setters>
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush>
                                    <GradientStop Color="Black" Offset="0" />
                                    <GradientStop Color="White" Offset="2" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Foreground" Value="White" />
                        <Setter Property="TextWrapping" Value="Wrap" />
                    </Style.Setters>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>

    <sys:String>coucouuuuuuuuuuuuu</sys:String>

</DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM