将集合绑定到ItemsControl控件时,会不加通告的在后台创建数据视图——位于数据源和绑定的控件之间。数据视图是进入数据源的窗口,可以跟踪当前项,并且支持各种功能,如排序、过滤、分组。
这些功能和数据对象本身是相互独立的,这意味着可在窗口的不同部分使用不同的方式绑定相同的数据。例如,可将同一个集合绑定到两个不同的列表,并对集合进行过滤以显示不同的记录。(来自于WPF编程宝典。我实测下来,绑定自同一个数据源的ItemsControl控件会共享一个View,当对该View进行筛选、排序时,会应用到所有绑定到该数据源的控件。)
获取视图的方法:
ListCollectionView? view = CollectionViewSource.GetDefaultView(filterListBox.ItemsSource) as ListCollectionView;
ListCollectionView? view = CollectionViewSource.GetDefaultView(Orders) as ListCollectionView;
可以看到,可以直接通过数据源来获取视图,这也表明,绑定到同一个数据源的控件会公用一个视图。
视图有MoveCurrentToPrevious()、MoveCurrentToNext() 方法,可以用于视图导航。
private void cmdPrev_Click(object sender, RoutedEventArgs e)
{
View?.MoveCurrentToPrevious();
}
private void cmdNext_Click(object sender, RoutedEventArgs e)
{
View?.MoveCurrentToNext();
}
private void view_CurrentChanged(object? sender, EventArgs e)
{
lblPosition.Text = "Record " + (View?.CurrentPosition + 1).ToString() + " of " + View?.Count.ToString();
cmdPrev.IsEnabled = View?.CurrentPosition > 0;
cmdNext.IsEnabled = View?.CurrentPosition
视图排序
View.SortDescriptions.Add(new SortDescription("Volume", ListSortDirection.Ascending));
View.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Descending));
视图分组
-
View.GroupDescriptions.Add(new PropertyGroupDescription("Volume"));
视图过滤
public class ProductByPriceFilterer
{
public ProductByPriceFilterer(decimal minimumPrice)
{
MinimumPrice = minimumPrice;
}
public decimal MinimumPrice { get; set; }
public bool FilterItem(Object item)
{
Order? order = item as Order;
if (order != null)
{
return order.Price > MinimumPrice;
}
return false;
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
View = (ListCollectionView)CollectionViewSource.GetDefaultView(Orders);
View.IsLiveFiltering = true;
View.LiveFilteringProperties.Add("Price");
}
public ObservableCollection Orders { get; set; } = new();
private ListCollectionView? View;
public decimal MinPrice { get; set; } = 200;
private ProductByPriceFilterer? filterer;
private void cmdFilter_Click(object sender, RoutedEventArgs e)
{
if (View != null)
{
filterer = new ProductByPriceFilterer(MinPrice);
View.Filter = new Predicate
完整代码文件:
MainWindow.xaml
-
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace DataView;
public class ViewModelBase : INotifyP服务器托管网ropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual bool SetProperty(ref T member, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer.Default.Equals(member, value))
{
return false;
}
服务器托管网 member = value;
OnPropertyChanged(propertyName);
return true;
}
}
public class Order : ViewModelBase
{
public decimal price = 0;
public decimal Price { get => price; set => SetProperty(ref price, value); }
public int volume = 0;
public int Volume { get => volume; set => SetProperty(ref volume, value); }
public DateTime orderDate = DateTime.Now;
public DateTime OrderDate { get => orderDate; set => SetProperty(ref orderDate, value); }
public string image = string.Empty;
public string Image { get => image; set => SetProperty(ref image, value); }
}
public class ProductByPriceFilterer
{
public ProductByPriceFilterer(decimal minimumPrice)
{
MinimumPrice = minimumPrice;
}
public decimal MinimumPrice { get; set; }
public bool FilterItem(Object item)
{
Order? order = item as Order;
if (order != null)
{
return order.Price > MinimumPrice;
}
return false;
}
}
public class PriceRangeProductGrouper : IValueConverter
{
public int GroupInterval { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
decimal price = (decimal)value;
if (price Orders { get; set; } = new();
private ListCollectionView? View;
private void cmdPrev_Click(object sender, RoutedEventArgs e)
{
View?.MoveCurrentToPrevious();
}
private void cmdNext_Click(object sender, RoutedEventArgs e)
{
View?.MoveCurrentToNext();
}
private void view_CurrentChanged(object? sender, EventArgs e)
{
lblPosition.Text = "Record " + (View?.CurrentPosition + 1).ToString() + " of " + View?.Count.ToString();
cmdPrev.IsEnabled = View?.CurrentPosition > 0;
cmdNext.IsEnabled = View?.CurrentPosition (filterer.FilterItem);
}
}
private void cmdRemoveFilter_Click(object sender, RoutedEventArgs e)
{
if (View != null)
{
View.Filter = null;
}
}
private void IncreaseButton_Click(object sender, RoutedEventArgs e)
{
foreach(var order in Orders)
{
order.Price *= 10;
}
}
private void DecreaseButton_Click(object sender, RoutedEventArgs e)
{
foreach (var order in Orders)
{
order.Price /= 10;
}
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
大家好,我是Edison。 最近,在使用CAP事件总线时,碰到了这样一个需求:微服务采用的是MongoDB,而且还是带身份验证 和 SSL根证书验证的。由于目前网上能找到的资料,都是不带身份验证的MongoDB,现在网络信息安全越来越被重视,那么就需要自己研究…