开发者

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>

How to limit selection background color to item width in WPF ListView?


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}" /> 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜