WPF CollectionViewSource ICollectionViewLiveShaping IsLiveSorting

FredGrit發表於2024-05-02
<Window x:Class="WpfApp82.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:local="clr-namespace:WpfApp82"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox ItemsSource="{Binding Items}" HorizontalAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Blue" BorderThickness="3">
                        <StackPanel>
                            <TextBlock Text="{Binding Name}"/>
                            <TextBlock Text="{Binding Value}"/>
                        </StackPanel>
                    </Border> 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Data;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApp82
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitItemsSource();
            this.DataContext = this;
        }

        private void InitItemsSource()
        {
            Items = new ObservableCollection<StockItem>();
            Random rnd = new Random();
            for(int i=0;i<10;i++)
            {
                Items.Add(new StockItem()
                {
                    Name = $"Item_{i + 1}",
                    Value = rnd.NextDouble() * rnd.Next(1, 10000)
                });
            }
            var view = CollectionViewSource.GetDefaultView(Items);
            view.SortDescriptions.Add(new SortDescription("Value", ListSortDirection.Ascending));
            var lv = (ICollectionViewLiveShaping)CollectionViewSource.GetDefaultView(Items);
            lv.IsLiveSorting = true; 
        }
         
        public ObservableCollection<StockItem> Items
        {
            get;set;
        }
    }

    public class StockItem : INotifyPropertyChanged
    { 
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        public string Name { get; set; }
        double _value;
        public double Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (_value!=value)
                {
                    _value = value;
                    OnPropertyChanged(nameof(Value));
                } 
            }
        }
    }
}

相關文章