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


    构建数据源

    1. <Window.Resources>
    2.         <x:ArrayExtension x:Key="stringList"
    3.                           xmlns="clr-namespace:System;assembly=mscorlib"
    4.                           Type="String">
    5.             <String>第一行String>
    6.             <String>第二行String>
    7.             <String>第三行String>
    8.             <String>第四行String>
    9.         x:ArrayExtension>
    10.     Window.Resources>

    1.      +     可以生效,背景色变颜色,

    ListView.ItemContainerStyle 中存放触发器

    ListView.View中存放数据

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

    如下所示:

    1. <ListView Grid.Row="0" Name="ItemList" ItemsSource="{StaticResource stringList}" >
    2. <ListView.ItemContainerStyle>
    3. <Style TargetType="{x:Type ListViewItem}">
    4. <Setter Property="FontSize" Value="15" />
    5. <Setter Property="Margin" Value="0,-40,0,40" />
    6. <Style.Triggers>
    7. <Trigger Property="IsSelected" Value="True">
    8. <Setter Property="Background" Value="#9C71B9" />
    9. <Setter Property="Foreground" Value="white" />
    10. Trigger>
    11. <Trigger Property="IsMouseOver" Value="True">
    12. <Setter Property="Background" Value="Green" />
    13. <Setter Property="Foreground" Value="white" />
    14. Trigger>
    15. <Trigger Property="IsEnabled" Value="False">
    16. <Setter Property="Background" Value="#757575" />
    17. <Setter Property="Foreground" Value="black" />
    18. Trigger>
    19. Style.Triggers>
    20. Style>
    21. ListView.ItemContainerStyle>
    22. <ListView.View>
    23. <GridView>
    24. <GridViewColumn>
    25. <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding}" />
    26. GridViewColumn>
    27. GridView>
    28. ListView.View>
    29. ListView>

    2.   +     不生效,背景色不改变颜色,

    ListView.ItemContainerStyle 中存放触发器

    ListView.ItemTemplate中存放数据

    如下所示:

    1. <ListView Name="ItemList" ItemsSource="{StaticResource stringList}">
    2. <ListView.ItemContainerStyle>
    3. <Style TargetType="{x:Type ListViewItem}">
    4. <Setter Property="FontSize" Value="15" />
    5. <Setter Property="Margin" Value="0,-40,0,40" />
    6. <Style.Triggers>
    7. <Trigger Property="IsSelected" Value="True">
    8. <Setter Property="Background" Value="#9C71B9" />
    9. <Setter Property="Foreground" Value="white" />
    10. Trigger>
    11. <Trigger Property="IsMouseOver" Value="True">
    12. <Setter Property="Background" Value="Green" />
    13. <Setter Property="Foreground" Value="white" />
    14. Trigger>
    15. Style.Triggers>
    16. Style>
    17. ListView.ItemContainerStyle>
    18. <ListView.ItemTemplate>
    19. <DataTemplate>
    20. <StackPanel >
    21. <StackPanel Orientation="Horizontal">
    22. <TextBlock Text="{Binding }" Height="30" Name="Label_Amount" Width="100" Foreground="White"/>
    23. StackPanel>
    24. StackPanel>
    25. DataTemplate>
    26. ListView.ItemTemplate>
    27. ListView>

    3.    只有ListView.ItemContainerStyle,可以生效

    数据放在里,触发器放在  ,如下所示:

    1. <ListView Width="300" Height="400" x:Name="celist" ItemsSource="{StaticResource stringList}" Background="Black">
    2. <ListView.ItemContainerStyle>
    3. <Style TargetType="ListViewItem">
    4. <Setter Property="Template">
    5. <Setter.Value>
    6. <ControlTemplate TargetType="ListBoxItem">
    7. <Border Background="{TemplateBinding Background}">
    8. <StackPanel >
    9. <StackPanel Orientation="Horizontal">
    10. <TextBlock Text="{Binding }" Height="30" Name="Label_Amount" Width="100" Foreground="White"/>
    11. StackPanel>
    12. <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
    13. <TextBlock Text="{Binding }" Name="Label_ShortDescription" Width="200" Foreground="White"/>
    14. StackPanel>
    15. StackPanel>
    16. Border>
    17. ControlTemplate>
    18. Setter.Value>
    19. Setter>
    20. <Style.Triggers>
    21. <Trigger Property="IsSelected" Value="True">
    22. <Setter Property="Background" Value="#9C71B9" />
    23. <Setter Property="Foreground" Value="white" />
    24. Trigger>
    25. <Trigger Property="IsMouseOver" Value="True">
    26. <Setter Property="Background" Value="Green" />
    27. <Setter Property="Foreground" Value="white" />
    28. Trigger>
    29. Style.Triggers>
    30. Style>
    31. ListView.ItemContainerStyle>
    32. ListView>

  • 相关阅读:
    什么是三高架构
    【附源码】计算机毕业设计SSM社区留守儿童帮扶系统
    Frida IOS 堆栈输出与IDA 对应
    web前端期末大作业 html+css+javascript汽车销售网站 学生网页设计实例 企业网站制作
    .NET Core Configuration 配置项知识点一网打尽!
    借助AI力量,赋能程序员高效工作与压力缓解之道
    RabbitMq
    c++ - 模板(一)
    适配器模式之SpringMvc源码HandlerAdapter
    Git: 仓库clone和用户配置
  • 原文地址:https://blog.csdn.net/orangapple/article/details/133883640