在wpf中限制网格视图的内容长度

时间:2017-02-22 10:03:30

标签: wpf listview gridview

我有一个列表视图,里面有网格视图。下面是我的输出和代码。

enter image description here

代码:

<ListView 
    Grid.Column="0"
    VerticalAlignment="Top"
    HorizontalAlignment="Center"
    FontSize="20"
    Width="Auto"
    Foreground="Black"
    x:Name="DataBackup" Margin="262,414,262,0">
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style>
                    <Setter Property="TextElement.FontSize" Value="20" />
                </Style>
            </GridView.ColumnHeaderContainerStyle>
            <GridViewColumn Header="Last Modified Date/Time" 
                                    DisplayMemberBinding="{Binding Date}" 
                                    Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource starWidthConverter}, ConverterParameter=1}"
                                    />
            <GridViewColumn Header="User"
                                    DisplayMemberBinding="{Binding User}"
                                    Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource starWidthConverter},ConverterParameter=2}"/>
            <GridViewColumn Header="Location"
                                    DisplayMemberBinding="{Binding Location}" 
                                    Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource starWidthConverter},ConverterParameter=3}"/>
        </GridView>
    </ListView.View>
    <local:SupportandMaintanceViewModel
                Date="02/07/2017 : 8:15"
                User="Admin"
                Location="Backup location will be shown here" />
</ListView>

现在我需要的是,我需要将内容限制为特定长度。 (例如,位置列中的内容将类似于“备份位置...”,如果我们将鼠标悬停在其上,则应将完整内容显示为工具提示)

任何人都可以分享你的想法吗?

1 个答案:

答案 0 :(得分:1)

如果列宽不足以显示所有内容,则会修剪文本:

<GridViewColumn Header="Location"
                Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource starWidthConverter},ConverterParameter=3}">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Location}" TextTrimming="CharacterEllipsis" ToolTip="{Binding Location}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>