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

WPF CollectionViewSource.GetDefaultView ICollectionViewLiveShaping IsLiveSorting

时间:2024-06-08 22:10:59浏览次数:20  
标签:ICollectionViewLiveShaping Windows items CollectionViewSource System new using W

<Window x:Class="WpfApp147.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:WpfApp147"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800"> 
    <Grid>
        <ListBox x:Name="lbx"  ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Margin="2" Padding="4" BorderThickness="3" BorderBrush="Blue">
                        <StackPanel Orientation="Horizontal" TextBlock.FontSize="20">
                            <TextBlock Foreground="Blue" FontWeight="Bold"
                                       Text="{Binding Name,StringFormat='Name:{0}'}"/>
                            <TextBlock Foreground="Red" Margin="10,0,0,0" FontSize="20"
                                       Text="{Binding Value,StringFormat='Value:{0}'}"/> 
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

//cs
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.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApp147
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    { 
        ObservableCollection<StockItem> _items;
        public MainWindow()
        {
            InitializeComponent();
            InitItems();
            var view = CollectionViewSource.GetDefaultView(_items);
            view.SortDescriptions.Add(new SortDescription(
                "Value", ListSortDirection.Ascending));
            var listView = (ICollectionViewLiveShaping)CollectionViewSource.GetDefaultView(_items);
            listView.IsLiveSorting = true;
            this.DataContext= view;
        }

        private void InitItems()
        {
            _items = new ObservableCollection<StockItem>();
            Random rnd = new Random();
            for (int i = 0; i < 100; i++)
            {
                _items.Add(new StockItem()
                {
                    Name = $"Name_{i + 1}",
                    Value = rnd.Next(1, 1000000)
                });
            }
        }
    }

    class StockItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public string Name { get; set; }
        double _value;
        public double Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }
    }
}

 

 

 

 

 

 

标签:ICollectionViewLiveShaping,Windows,items,CollectionViewSource,System,new,using,W
From: https://www.cnblogs.com/Fred1987/p/18239022

相关文章

  • WPF Image zoomin zoomout move
    //xaml<Windowx:Class="WpfApp145.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF ListBox ListBox.ItemTemplate DataTemplate
    <Windowx:Class="WpfApp144.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 master detail view
    <Windowx:Class="WpfApp143.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 RepeatButton
    是一个特殊的按钮,用于在用户长按或连续点击时重复执行特定动作。它通常用于需要重复执行某个操作的场景。常用属性描述Delay用于获取或设置RepeatButton在开始重复之前被按下时等待的时间(以毫秒为单位)。该值必须为非负数。Interval用于获取或设置开始重复后重复之......
  • WPF button mouseover background change color
    <Applicationx:Class="WpfApp142.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-nam......
  • 在WPF程序中如何实现主题,字体的切换
    在wpf程序中如何实现主题变换,首先我们需要先思考主题切换都更改了一些什么,无非就是字体颜色,背景颜色等一些资源当我们了解了主题变换的本质后就能够进一步去实现它,众所周知,在wpf中我们可以通过资源绑定的形式去实现颜色,字体,样式等一些资源绑定,在wpf中如果有相同的key通常会绑......
  • 【WPF】Dispatcher 与消息循环
    这一期的话题有点深奥,不过按照老周一向的作风,尽量讲一些人鬼都能懂的知识。咱们先来整个小活开开胃,这个小活其实老周在N年前写过水文的,常阅读老周水文的伙伴可能还记得。通常,咱们按照正常思路构建的应用程序,第一个启动的线程为主线程,而且还是UI线程(当然,WPF默认会创建辅助线......
  • WPF grid column resize via GridSpitter, when you can drag to enlarge or shrink t
    <Windowx:Class="WpfApp137.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,图表控件
    开源代码地址:https://github.com/bearhanQ/WPFFramework;QQ群:332035933;<UserControlx:Class="WpfBootstrap.View.ChartsView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://......
  • wpf datagrid绑定行选中状态
    样式如下<DataGridMargin="0,6,0,0"HeadersVisibility="All"RowHeaderWidth="60"HorizontalScrollBarVisibility="Visible"AutoGenerateColumns="False"ItemsSource="{BindingDispl......