【问题标题】:Selection style for ListView with a WrapPanel带有 WrapPanel 的 ListView 的选择样式
【发布时间】:2016-02-02 18:47:48
【问题描述】:

我使用ListViewWrapPanel 作为它的ItemsPanel。我需要更改所选项目的样式 - 使用不同的背景颜色并在所选项目周围添加边框,就像 Windows 7 的资源管理器一样。

<ListView ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>

    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Margin="10">
                <Rectangle Width="100" Height="100" Fill="Pink" />
                <TextBlock Text="{Binding Caption}" Margin="0,10,0,0" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: c# .net wpf listview listviewitem


    【解决方案1】:

    只需为默认类型和选定类型设计ControlTemplate。然后可以在 ListView 中的任何项目被选中时设置 Selected ControlTemplate,否则保持默认类型。

        <Window.Resources>
        <ControlTemplate  x:Key="DEFAULT">
            <StackPanel Orientation="Vertical" Margin="10">
                <Rectangle Width="100" Height="100" Fill="Green" />
                <TextBlock Text="{Binding Caption}" Margin="0,10,0,0" />
            </StackPanel>
        </ControlTemplate>
        <ControlTemplate  x:Key="SELECTED_TYPE">
            <Border BorderBrush="Gray" BorderThickness="1">
            <StackPanel Orientation="Vertical" Margin="10">
                <Rectangle Width="100" Height="100" Fill="Pink" />
                <TextBlock Text="{Binding Caption}" Margin="0,10,0,0" />
            </StackPanel>
            </Border>
        </ControlTemplate>
    
        <Style x:Key="ListItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="White"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Template" Value="{StaticResource SELECTED_TYPE}"/>
                    <Setter Property="Background" Value="Orange"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="False">
                    <Setter Property="Template" Value="{StaticResource DEFAULT}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <ListView ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
              ItemContainerStyle="{StaticResource ListItemStyle}">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多