代码之家  ›  专栏  ›  技术社区  ›  Meh

如何在WPF列表视图中将所选内容的背景色限制为项目宽度?

  •  1
  • Meh  · 技术社区  · 15 年前

    如何使选择的颜色(此处为蓝色)停止在文件名的末尾,而不是一直到 ListView ?

    我把橙色的背景放在这个项目上 StackPanel 不是因为它填满了我最初想的整个宽度。

    <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

    1 回复  |  直到 15 年前
        1
  •  3
  •   Ray Burns    15 年前

    将ItemContainerStyle更改为incude HorizontalAlignment=“Left”。这样,项目容器将缩小到项目的大小,而不是填充整个列。

    <Style x:Key="LeftAligned">
      <Setter Property="FrameworkElement.HorizontalAlignment" Value="Left" />
    </Style>
    
    
    ...
    
    <ListView ItemTemplate="{StaticResource IconTextItemTemplate}"
              ItemContainerStyle="{StaticResource LeftAligned}" />