• WPF列表样式


    WPF的数据绑定系统自动生成列表项对象,为单个项应用所需的样式不是很容易。解决方案是ItemContainerStyle 属性。如果设置了ItemContainerStyle 属性,当创建列表项时,列表控件会将其向下传递给每个项。对于ListBox控件,每个项有ListBoxItem 对象表示,对于CombBox 控件,则对应是 CombBoxItem。

    交替条目样式

    WPF通过两个属性为交替项提供内置支持:AlternationCount 和 AlternationIndex。

    1. "myGrid">
    2. "100"/>
    3. "0" Grid.Column="0" ItemContainerStyle="{StaticResource listBoxItemStyle}" ItemsSource="{Binding Path=Orders}" AlternationCount="2" DisplayMemberPath="Price"/>

    也可以直接将样式设置到ListBox层次

    1. "myGrid">
    2. "100"/>
    3. "1" Grid.Column="0" Style="{StaticResource checkBoxListStyle}" ItemsSource="{Binding Path=Orders}" DisplayMemberPath="Price" Name="checkButtonListBox"/>

    样式选择器

    可以使用样式选择器来为不同的子项提供不同的样式,自定义样式选择器需要继承自 StyleSelector 类,需要重写 SelectStyle() 方法。

    1. public class SingleCriteriaHighlightStyleSelector : StyleSelector
    2. {
    3. public Style DefaultStyle { get; set; }
    4. public Style HighlightStyle { get; set; }
    5. public string PropertyToEvaluate { get; set; }
    6. public string PropertyValueToHighlight { get; set; }
    7. public override Style SelectStyle(object item, DependencyObject container)
    8. {
    9. Order order = (Order)item;
    10. if (order.Price > 1000)
    11. {
    12. return HighlightStyle;
    13. }
    14. else
    15. {
    16. return DefaultStyle;
    17. }
    18. }
    19. }

    完整的代码文件:

    MainWindow.xaml

    1. "ListBoxStyle.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:ListBoxStyle"
    7. mc:Ignorable="d"
    8. Title="MainWindow" Height="450" Width="800">
    9. "myGrid">
    10. "100"/>
    11. "0" Grid.Column="0" ItemContainerStyle="{StaticResource listBoxItemStyle}" ItemsSource="{Binding Path=Orders}" AlternationCount="3" DisplayMemberPath="Price"/>
    12. "0" Grid.Column="1" ItemContainerStyle="{StaticResource radioButtonListStyle}" ItemsSource="{Binding Path=Orders}" DisplayMemberPath="Price" Name="radioButtonListBox"/>
    13. "1" Grid.Column="0" Style="{StaticResource checkBoxListStyle}" ItemsSource="{Binding Path=Orders}" DisplayMemberPath="Price" Name="checkButtonListBox"/>
    14. "1" Grid.Column="1" ItemsSource="{Binding Path=Orders}" DisplayMemberPath="Price" Name="styleSelectorListBox">
    15. "{StaticResource DefaultStyle}" HighlightStyle="{StaticResource HighlightStyle}">

    MainWindow.xaml.cs

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Collections.ObjectModel;
    4. using System.ComponentModel;
    5. using System.Linq;
    6. using System.Reflection;
    7. using System.Runtime.CompilerServices;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows;
    11. using System.Windows.Controls;
    12. using System.Windows.Data;
    13. using System.Windows.Documents;
    14. using System.Windows.Input;
    15. using System.Windows.Media;
    16. using System.Windows.Media.Imaging;
    17. using System.Windows.Navigation;
    18. using System.Windows.Shapes;
    19. namespace ListBoxStyle;
    20. public class ViewModelBase : INotifyPropertyChanged
    21. {
    22. public event PropertyChangedEventHandler? PropertyChanged;
    23. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    24. {
    25. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    26. }
    27. protected virtual bool SetProperty<T>(ref T member, T value, [CallerMemberName] string? propertyName = null)
    28. {
    29. if (EqualityComparer.Default.Equals(member, value))
    30. {
    31. return false;
    32. }
    33. member = value;
    34. OnPropertyChanged(propertyName);
    35. return true;
    36. }
    37. }
    38. public class Order : ViewModelBase
    39. {
    40. public decimal price = 0;
    41. public decimal Price { get => price; set => SetProperty(ref price, value); }
    42. public int volume = 0;
    43. public int Volume { get => volume; set => SetProperty(ref volume, value); }
    44. public DateTime orderDate = DateTime.Now;
    45. public DateTime OrderDate { get => orderDate; set => SetProperty(ref orderDate, value); }
    46. public string image = string.Empty;
    47. public string Image { get => image; set => SetProperty(ref image, value); }
    48. }
    49. public class SingleCriteriaHighlightStyleSelector : StyleSelector
    50. {
    51. public Style DefaultStyle { get; set; }
    52. public Style HighlightStyle { get; set; }
    53. public string PropertyToEvaluate { get; set; }
    54. public string PropertyValueToHighlight { get; set; }
    55. public override Style SelectStyle(object item, DependencyObject container)
    56. {
    57. Order order = (Order)item;
    58. if (order.Price > 1000)
    59. {
    60. return HighlightStyle;
    61. }
    62. else
    63. {
    64. return DefaultStyle;
    65. }
    66. }
    67. }
    68. public partial class MainWindow : Window
    69. {
    70. public MainWindow()
    71. {
    72. InitializeComponent();
    73. myGrid.DataContext = this;
    74. Order order1 = new Order();
    75. Order order2 = new Order();
    76. Order order3 = new Order();
    77. Order order4 = new Order();
    78. order1.Price = 100;
    79. order1.Volume = 10;
    80. order2.Price = 1000;
    81. order2.Volume = 100;
    82. order3.Price = 10000;
    83. order3.Volume = 1000;
    84. order4.Price = 100000;
    85. order4.Volume = 10000;
    86. Orders.Add(order1);
    87. Orders.Add(order2);
    88. Orders.Add(order3);
    89. Orders.Add(order4);
    90. }
    91. public ObservableCollection Orders {get; set;} = new ();
    92. private void Button_Click(object sender, RoutedEventArgs e)
    93. {
    94. string message = "";
    95. if(radioButtonListBox.SelectedItem != null)
    96. {
    97. Order order = (Order)radioButtonListBox.SelectedItem;
    98. message = order.Price.ToString();
    99. }
    100. message += "\n";
    101. foreach (var selectedItem in checkButtonListBox.SelectedItems)
    102. {
    103. Order order = (Order)selectedItem;
    104. message += order.Price.ToString() + " ";
    105. }
    106. MessageBox.Show(message);
    107. }
    108. private void Button_Click_1(object sender, RoutedEventArgs e)
    109. {
    110. Orders[1].Price = 50000;
    111. StyleSelector selector = styleSelectorListBox.ItemContainerStyleSelector;
    112. styleSelectorListBox.ItemContainerStyleSelector = null;
    113. styleSelectorListBox.ItemContainerStyleSelector = selector;
    114. }
    115. }

  • 相关阅读:
    DAX Studio3正式发布了!
    Java设计模式之职责链模式
    阅读LINGO-1: Exploring Natural Language for Autonomous Driving
    kubeadm v1.20 部署K8S 集群架构
    大前端面试注意要点
    JAVA概述
    计算机网络常见面试题
    医疗项目的需求分析以及开发流程
    Linux文件属性操作函数
    Jenkins环境部署与任务构建
  • 原文地址:https://blog.csdn.net/xunmeng2002/article/details/132584694