WPF自动填充文本块,列表视图中包含省略号

时间:2016-04-22 08:59:52

标签: c# wpf listview

它应该看起来像图像enter image description here

我有一个自定义DataTemplate的列表视图。我必须在Description TextBlock中使用省略号自动填充所有可用空间。

你知道我怎么能这样做吗?

<ListView ItemsSource="{Binding Items}" HorizontalAlignment="Stretch">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Width="16" Height="16" Source="{Binding Type, Converter={StaticResource NodeIconConverter}}" />
                <TextBlock Grid.Column="1" Text="{Binding Tittle}" ToolTip="{Binding Description}" />
                <Image Grid.Column="2" Width="16" Height="16" Source="{Binding Status, Converter={StaticResource NodeStatusConverter}}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

1 个答案:

答案 0 :(得分:2)

您可以像下面一样更新DataTemplate,在空格之间加上虚线:

    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="Auto" />
                 <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="20" />
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Width="16" Height="16" Source="{Binding Type, Converter={StaticResource NodeIconConverter}}" />
            <TextBlock Grid.Column="1" Text="{Binding Tittle}" ToolTip="{Binding Description}" />
            <Rectangle Grid.Column="2" Margin="2,5,2,0" Stroke="#FF000000" Height="1" StrokeThickness="1" StrokeDashArray="1 4"
                                                   SnapsToDevicePixels="True"/>
            <Image Grid.Column="3" Width="16" Height="16" Source="{Binding Status, Converter={StaticResource NodeStatusConverter}}" />
        </Grid>
    </DataTemplate>