TheLastGIS
TheLastGIS

Reputation: 434

Text not wrapping in ListViewItem

The ListViewItems are getting their width from the TextBlock element. I want the ListViewItem to fit to the ListView. I have seen in other posts that making HorizontalContentAlignment="Stretch" will fix the issue, but my problem persists. The second TextBlock element in the ItemTemplate is set to WrapWithOverflow.

Sources:

http://www.teixeira-soft.com/bluescreen/2016/03/21/c-how-to-make-a-panel-within-a-datatemplate-fill-the-entire-width-of-a-listview-or-itenscontrol-derivative/

Code:

<UserControl x:Class="XXXX.Views.XXXXView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:cal="http://www.caliburnproject.org"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="{StaticResource BlackBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="32*"/>
            <ColumnDefinition Width="6*"/>
            <ColumnDefinition Width="32*"/>
        </Grid.ColumnDefinitions>    
        <DockPanel Grid.Column="1">
            <ListView Grid.Column="1"
                      x:Name="UniqueShortDescriptions" 
                      VerticalAlignment="Stretch"
                      ItemsSource="{Binding UniqueShortDescriptions}"
                      SelectedItem="{Binding SelectedUniqueShortDescriptions}"
                      cal:Message.Attach="[Event SelectionChanged] = [Action SelectionChanged($this, $eventArgs)]">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Navy"
                                BorderThickness="3"
                                Margin="3"
                                Padding="2">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.RowSpan="3"
                                           Grid.Column="0"
                                           Text="{Binding Path=Date}" 
                                           Foreground="Black"
                                           VerticalAlignment="Center"
                                           TextBlock.FontWeight="ExtraBold"/>
                                <TextBlock Grid.Row="0"
                                           Grid.Column="1"
                                           Text="{Binding Path=LongDescription}" Foreground="{StaticResource BlackBrush}"
                                               TextWrapping="WrapWithOverflow"/>
                                <!--<TextBlock Text="{Binding Path=ShortExpenseDescription}" Foreground="{StaticResource BlackBrush}"/>-->
                                <TextBlock Grid.Row="1"
                                           Grid.Column="1"
                                           Text="{Binding Path=Cost}" Foreground="{StaticResource BlackBrush}"/>
                                <TextBlock Grid.Row="2"
                                           Grid.Column="1"
                                           Text="{Binding Path=Source}" Foreground="{StaticResource BlackBrush}"/>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </DockPanel>
    </Grid>
</UserControl>

Upvotes: 2

Views: 1612

Answers (1)

KTCheek
KTCheek

Reputation: 319

I got the code you posted to work by simply disabling horizontal scrolling of the ListView.

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...

Upvotes: 2

Related Questions