首页 > 其他分享 >WPF CollectionViewSource ICollectionViewLiveShaping IsLiveSorting

WPF CollectionViewSource ICollectionViewLiveShaping IsLiveSorting

时间:2024-05-02 17:44:07浏览次数:25  
标签:ICollectionViewLiveShaping Windows CollectionViewSource System value Collections

<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));
                } 
            }
        }
    }
}

 

标签:ICollectionViewLiveShaping,Windows,CollectionViewSource,System,value,Collections
From: https://www.cnblogs.com/Fred1987/p/18170376

相关文章

  • WPF DataContext="{Binding SelectedItem,ElementName=_master}"
    <Windowx:Class="WpfApp80.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF DataTemplate DataTrigger
    <Windowx:Class="WpfApp79.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF多语言支持:简单灵活的动态切换,让你的程序支持多国语言
     概述:本示例演示了在WPF应用程序中实现多语言支持的详细步骤。通过资源字典和数据绑定,以及使用语言管理器类,应用程序能够在运行时动态切换语言。这种方法使得多语言支持更加灵活,便于维护,同时提供清晰的代码结构。在WPF中实现多语言的一种常见方法是使用资源字典和数据绑定。......
  • WPF DataTemplate DataTemplateSelector
    //xaml<Windowx:Class="WpfApp78.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF DataTemplate DataType
    //xaml<Windowx:Class="WpfApp77.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF SetProperty to implement compare,assign and notify
    protectedvoidSetProperty<T>(refTfield,Tvalue,[CallerMemberName]stringpropName=null){if(!EqualityComparer<T>.Default.Equals(field,value)){field=value;varhandler=PropertyChanged;if(handler!=nul......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • WPF 后台设置DataGrid选中多行
    1privatevoidSetSelectIndex(List<int>listIndex)2{3try4{5foreach(variinlistIndex)6{7if(i>=datagridSig.Items.Count)8......
  • WPF pass event method to viewmodel via Interaction:CallMethodAction,TargetObject
    <Windowx:Class="WpfApp71.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 【转】[WPF] 复制文本到剪贴板
    来自:阿里的通义灵码以下是几种常见的复制数据类型到剪切板的方法:复制文本到剪切板usingSystem.Windows.Forms;//对于WindowsForms应用//或者usingSystem.Windows;//对于WPF应用publicvoidCopyTextToClipboard(stringtext){//确保在UI线程中操作剪切板......