How to limit selection background color to item width in WPF ListView?
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 tho开发者_JS百科ught.
<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>
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}" />
精彩评论