开发者

WPF: how can I have ListViewItems fit the window width and use TextEllipsis

I'm currently working with a ListView. Its ListViewItems consist of a left-aligned TextBlock and a right-aligned Button:

WPF: how can I have ListViewItems fit the window width and use TextEllipsis

Now, I'd like to have my Items always display the Button and shorten the TextBlock accordingly, so they don't need to display a ScrollBar:

WPF: how can I have ListViewItems fit the window width and use TextEllipsis

Unfortunately, right now that doesn't work:

WPF: how can I have ListViewItems fit the window width and use TextEllipsis

What can I do to make it work? Here's my example code:

<Window x:Class="JansWpfTestUmgebung.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListView x:Name="AllItemsList"
              HorizontalContentAlignment="Stretch"
              HorizontalAlignment="Stretch">
        <ListViewItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"
                       TextTrimming="CharacterEllipsis" 
                       Text="Item 1 with a very long description"/>
            <Button Grid.Column="1"
                    Content="Modify" />
        <开发者_运维知识库/Grid>
    </ListViewItem>
        <ListViewItem>Item 2</ListViewItem>
        <ListViewItem>Item 3</ListViewItem>
    </ListView>
</Window>

Thanks for any hints! :-)


You can do this by forcing a size on the button column:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="JansWpfTestUmgebung.MainWindow"
        d:DesignWidth="394">
    <ListView x:Name="AllItemsList"
              HorizontalContentAlignment="Stretch"
              HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListViewItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="45" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"
                TextTrimming="CharacterEllipsis" 
                Text="Item 1 with a very long description"/>
            <Button Grid.Column="1"
                    Content="Modify"/>
        </Grid>
    </ListViewItem>
        <ListViewItem Content="Item 2"/>
        <ListViewItem Content="Item 3"/>
    </ListView>
</Window>

The way it was, It does its best to keep the * sized column as big as possible, at the expense of the Auto sized column. But a fixed size will win out against a * sized.


The Most important Parts are these

<ListView HorizontalContentAlignment="Stretch"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>

That way your ListViewItems will fill the space, and the disabled horizontal scroll bar will prevent them from taking up more space.

My code looks like this

<ListView HorizontalContentAlignment="Stretch"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          ItemsSource="{Binding MyItemSourceList}"
          Width="{Binding ActualWidth, ElementName=SearchBox}"
          >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" 
                               Text="{Binding Name}" 
                               TextTrimming="CharacterEllipsis"/>
                    <TextBlock Grid.Column="1" 
                               Text="{Binding Tag}" 
                               HorizontalAlignment="Right" 
                               FontWeight="Bold" 
                               Foreground="LightGray"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜