防止列表视图自动拉伸

时间:2017-07-31 15:10:35

标签: c# xaml uwp

我的列表视图:

<ListView x:Name="LV_" Margin="0,60,0,0" IsItemClickEnabled="True" SelectionMode="None">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

列定义(在代码中设置):

new ColumnDefinition[]
{
    new ColumnDefinition() { Width = new GridLength(150) },
    new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) },
    new ColumnDefinition() { Width = new GridLength(100) },
    new ColumnDefinition() { Width = new GridLength(100) },
    new ColumnDefinition() { Width = new GridLength(150) },
    new ColumnDefinition() { Width = new GridLength(100) }
}

当我现在加载一个项目列表,其中第二个项目(宽度为1 *)是一个长字符串时,列表会在页面的边界上延伸(到文本的长度)。

如果仍然有一个使用页面整个宽度的列表视图,我怎么能阻止这种情况(并显示例如'abcabcab ...')?

修改:

生成的项目模板:

    <DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="150" MinWidth="0" />
    <ColumnDefinition Width="150" MinWidth="0" />
    <ColumnDefinition Width="100" MinWidth="0" />
    <ColumnDefinition Width="100" MinWidth="0" />
    <ColumnDefinition Width="100" MinWidth="0" />
    <ColumnDefinition Width="100" MinWidth="0" />
    <ColumnDefinition Width="100" MinWidth="0" />
    <ColumnDefinition Width="auto" MinWidth="0" />
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" BorderThickness="0" Width="5" Background="{Binding BackgroundBrush}" HorizontalAlignment="Left" Margin="-12,0,0,0" />
    <TextBlock Grid.Column="0" Text="{Binding Day}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="1" Text="{Binding Department}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="2" Text="{Binding Begin}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="3" Text="{Binding End}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="4" Text="{Binding Allocation}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="5" Text="{Binding Position}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="6" Text="{Binding ServiceType}" Foreground="{ StaticResource TextBrushLight}" />
    <TextBlock Grid.Column="7" Text="{Binding Info}" Foreground="{ StaticResource TextBrushLight}" />
    </Grid>
    </DataTemplate>

1 个答案:

答案 0 :(得分:0)

为了添加TextTrimming="CharacterEllipsis"设置,要解决此问题,一种方法是在TextBlock之外添加边框,并根据{{设置TextBlock的宽度边界的1}}属性。

ActualWidth

完整的XAML:

<Border Grid.Column="1" BorderBrush="Transparent">
    <TextBlock Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=.ActualWidth}" Text="{Binding Department}" TextTrimming="CharacterEllipsis" Foreground="{StaticResource TextBrushLight}"/>
</Border>

<强>截图:

enter image description here