WPF ListView 鼠标点击,移动改变背景色不启作用

构建数据源

XML 复制代码
 <Window.Resources>
        <x:ArrayExtension x:Key="stringList"
                          xmlns="clr-namespace:System;assembly=mscorlib"
                          Type="String">
            <String>第一行</String>
            <String>第二行</String>
            <String>第三行</String>
            <String>第四行</String>
        </x:ArrayExtension>
    </Window.Resources>
  1. <ListView.ItemContainerStyle> + <ListView.View> 可以生效,背景色变颜色,

ListView.ItemContainerStyle 中存放触发器

ListView.View中存放数据

这样只能按列显示多列多行,不能多行多列作为一项显示

如下所示:

XML 复制代码
 <ListView Grid.Row="0" Name="ItemList" ItemsSource="{StaticResource stringList}" >
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="FontSize" Value="15" />
                    <Setter Property="Margin" Value="0,-40,0,40" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#9C71B9" />
                            <Setter Property="Foreground" Value="white" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                            <Setter Property="Foreground" Value="white" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="#757575" />
                            <Setter Property="Foreground" Value="black" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding}" />
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
  1. <ListView.ItemContainerStyle> + <ListView.ItemTemplate> 不生效,背景色不改变颜色,

ListView.ItemContainerStyle 中存放触发器

ListView.ItemTemplate中存放数据

如下所示:

XML 复制代码
        <ListView  Name="ItemList" ItemsSource="{StaticResource stringList}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="FontSize" Value="15" />
                    <Setter Property="Margin" Value="0,-40,0,40" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#9C71B9" />
                            <Setter Property="Foreground" Value="white" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                            <Setter Property="Foreground" Value="white" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel >
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding }" Height="30"  Name="Label_Amount" Width="100" Foreground="White"/>

                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
  1. 只有ListView.ItemContainerStyle,可以生效

数据放在<Setter Property="Template">里,触发器放在 <Style.Triggers>,如下所示:

XML 复制代码
<ListView   Width="300" Height="400" x:Name="celist" ItemsSource="{StaticResource stringList}" Background="Black">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border Background="{TemplateBinding Background}">
                                    <StackPanel >
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding }" Height="30"  Name="Label_Amount" Width="100" Foreground="White"/>
                                           
                                        </StackPanel>
                                        <StackPanel HorizontalAlignment="Left"  Orientation="Horizontal">
                                            <TextBlock Text="{Binding }" Name="Label_ShortDescription" Width="200" Foreground="White"/>

                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>

                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#9C71B9" />
                            <Setter Property="Foreground" Value="white" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                            <Setter Property="Foreground" Value="white" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
相关推荐
暮雪倾风10 小时前
【WPF开发】超级详细的“文件选择”(附带示例工程)
windows·wpf
明耀15 小时前
WPF RadioButton 绑定boolean值
c#·wpf
郭二哈15 小时前
C++——list
开发语言·c++·list
暮雪倾风16 小时前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
cdut_suye2 天前
STL之list篇(下)(从底层分析实现list容器,逐步剥开list的外表)
开发语言·数据结构·c++·学习·算法·stl·list
芝麻科技2 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
编程版小新2 天前
C++初阶:STL详解(七)——list的模拟实现
开发语言·c++·学习·迭代器·list·list的模拟实现
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
T-ang.3 天前
第九章---for循环及在STL的应用(vector\map\set\list\for_each)、嵌套while、while 统一输出、do-while
开发语言·数据结构·c++·学习·算法·list·改行学it
月色不够温柔ii3 天前
C++容器之list基本使用
开发语言·数据结构·c++·链表·list