Question

How can I make the selection color (blue here) stop at the end of the filename instead of going all the way to the edge of the ListView?

I put the orange background on the item StackPanel to show that it is not because it fills the whole width which I first thought.

<Expander Header="Project">
    <Expander.Resources>
        <DataTemplate x:Key="IconTextItemTemplate">
            <StackPanel Orientation="Horizontal" Background="Orange">
                <Image Source="{Binding icon}"/>
                <TextBlock Text="{Binding text}"/>
            </StackPanel>
        </DataTemplate>
    </Expander.Resources>
    <ListView ItemTemplate="{StaticResource IconTextItemTemplate}"/>
</Expander>

alt text

Was it helpful?

Solution

Change the ItemContainerStyle to incude HorizontalAlignment="Left". That way the item container will shrink to the size of the item instead of filling the entire column.

<Style x:Key="LeftAligned">
  <Setter Property="FrameworkElement.HorizontalAlignment" Value="Left" />
</Style>


...

<ListView ItemTemplate="{StaticResource IconTextItemTemplate}"
          ItemContainerStyle="{StaticResource LeftAligned}" /> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top