WPF数据视图

将集合绑定到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?.Count - 1;}

视图排序

View.SortDescriptions.Add(new SortDescription("Volume", ListSortDirection.Ascending));
View.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Descending));

视图分组

<ListBox x:Name="groupListBox" ItemsSource="{Binding Path=Orders}"><ListBox.ItemTemplate><DataTemplate><TextBlock><TextBlock Text="{Binding Price}"></TextBlock> - <TextBlock Text="{Binding Volume}"></TextBlock></TextBlock></DataTemplate></ListBox.ItemTemplate><ListBox.GroupStyle><GroupStyle><GroupStyle.HeaderTemplate><DataTemplate><TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White" Background="LightGreen" Margin="0,5,0,0" Padding="3"/></DataTemplate></GroupStyle.HeaderTemplate></GroupStyle></ListBox.GroupStyle>
</ListBox>
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<Order> 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<object>(filterer.FilterItem);}}private void cmdRemoveFilter_Click(object sender, RoutedEventArgs e){if (View != null){View.Filter = null;}}
}

完整代码文件:

MainWindow.xaml

<Window x:Class="DataView.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:DataView"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid Name="myGrid"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition Height="Auto"/></Grid.RowDefinitions><StackPanel Grid.Row="0" Grid.Column="0" ><StackPanel Orientation="Horizontal"><Button Name="cmdPrev" Click="cmdPrev_Click">&lt;</Button><TextBlock Name="lblPosition" VerticalAlignment="Center"></TextBlock><Button Name="cmdNext" Click="cmdNext_Click">&gt;</Button></StackPanel><ListBox x:Name="navigateListBox" DisplayMemberPath="Price" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Orders}"/></StackPanel><StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"><Grid><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Label Grid.Row="0" Grid.Column="0">Price > Than</Label><TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=MinPrice}"></TextBox><Button Grid.Row="1" Grid.Column="0" Click="cmdFilter_Click">Filter</Button><Button Grid.Row="1" Grid.Column="1" Click="cmdRemoveFilter_Click">Remove Filter</Button></Grid><ListBox Name="filterListBox" DisplayMemberPath="Price" ItemsSource="{Binding Path=Orders}"/></StackPanel><StackPanel Grid.Row="1" Grid.Column="0"><ListBox x:Name="groupListBox" ItemsSource="{Binding Path=Orders}"><ListBox.ItemTemplate><DataTemplate><TextBlock><TextBlock Text="{Binding Price}"></TextBlock> - <TextBlock Text="{Binding Volume}"></TextBlock></TextBlock></DataTemplate></ListBox.ItemTemplate><ListBox.GroupStyle><GroupStyle><GroupStyle.HeaderTemplate><DataTemplate><TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White" Background="LightGreen" Margin="0,5,0,0" Padding="3"/></DataTemplate></GroupStyle.HeaderTemplate></GroupStyle></ListBox.GroupStyle></ListBox></StackPanel><Button Grid.Row="2" Grid.Column="0" Content="Increase Price" Click="IncreaseButton_Click"/><Button Grid.Row="2" Grid.Column="1" Content="Decrease Price" Click="DecreaseButton_Click"/></Grid>
</Window>

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 : INotifyPropertyChanged
{public event PropertyChangedEventHandler? PropertyChanged;protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}protected virtual bool SetProperty<T>(ref T member, T value, [CallerMemberName] string? propertyName = null){if (EqualityComparer<T>.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 < GroupInterval){return string.Format("Less than {0:C}", GroupInterval);}else{int interval = (int)price / GroupInterval;int lowerLimit = interval * GroupInterval;int upperLimit = (interval + 1) * GroupInterval;return string.Format("{0:C} to {1:C}", lowerLimit, upperLimit);}}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotSupportedException("This converter is for grouping only.");}
}
public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();myGrid.DataContext = this;InitOrders();InitView();}public void InitOrders(){Order order1 = new Order();Order order2 = new Order();Order order3 = new Order();Order order4 = new Order();order1.Price = 100;order1.Volume = 100;order1.Image = "image1.gif";order2.Price = 1000;order2.Volume = 100;order2.Image = "image2.gif";order3.Price = 10000;order3.Volume = 10000;order3.Image = "image3.gif";order4.Price = 100000;order4.Volume = 10000;order4.Image = "image4.gif";Orders.Add(order1);Orders.Add(order2);Orders.Add(order3);Orders.Add(order4);}private void InitView(){View = (ListCollectionView)CollectionViewSource.GetDefaultView(Orders);if(View != null){View.CurrentChanged += new EventHandler(view_CurrentChanged);View.SortDescriptions.Add(new SortDescription("Volume", ListSortDirection.Ascending));View.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Descending));View.GroupDescriptions.Add(new PropertyGroupDescription("Volume"));View.IsLiveFiltering = true;View.LiveFilteringProperties.Add("Price");}}public ObservableCollection<Order> 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 < View?.Count - 1;}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<object>(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.mzph.cn/news/64115.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【计算机网络】OSI 七层网络参考模型

OSI&#xff08;Open Systems Interconnection&#xff09;七层网络参考模型是一种用于描述计算机网络通信的框架&#xff0c;将网络通信划分为七个不同的层次&#xff0c;每个层次负责不同的功能。 以下为 OSI 七层网络参考模型的简单表格&#xff1a; --------------------…

maven的依赖下载不下来的几种解决方法

前言 每次部署测试环境&#xff0c;从代码库拉取代码&#xff0c;都会出现缺少包的情况。然后找开发一通调试&#xff0c;到处拷包。 方案一&#xff1a;pom文件注释/取消注释 注释掉pom.xml里的报红色的依赖&#xff08;同时可以把本地maven库repo里对应的包删除&#xff09;&…

一款不能错过的Git客户端:Fork for Mac,让你的代码管理更便捷

Fork for Mac是一款强大的Git客户端&#xff0c;让用户在Mac电脑上更方便地进行版本控制和代码管理。它具有以下特点&#xff1a; 易用性&#xff1a;Fork for Mac界面简洁明了&#xff0c;操作简单易懂&#xff0c;让用户可以快速上手。功能强大&#xff1a;支持各种Git功能&…

Mac软件删除方法?如何删除不会有残留

Mac电脑如果有太多无用的应用程序&#xff0c;很有可能会拖垮Mac系统的运行速度。因此&#xff0c;卸载电脑中无用的软件是优化Mac系统运行速度的最佳方式之一。Mac卸载应用程序的方式是和Windows有很大的区别&#xff0c;特别对于Mac新用户来说&#xff0c;如何无残留的卸载删…

Java8异步类CompletableFuture详解

1、前言 学习java基础时候多线程使用我们首先学习的 Runable 、Future 、 Thread 、ExecutorService、Callable等相关类&#xff0c;在我们日常工作或者学习中有些场景并不满足我们需求&#xff0c;JDK8引入了一个新的类 CompletableFuture 来解决之前得问题&#xff0c; Comp…

【Latex】使用技能站:(三)使用 Vscode 配置 LaTeX

使用 Vscode 配置 LaTeX 引言1 安装texlive2 安装vscode2.1 插件安装2.2 配置 3 安装SumatraPdf3.1 vscode配置3.2 配置反向搜索 引言 安装texlive 安装vscode 安装SumatraPdf 1 安装texlive 在线LaTeX编辑器&#xff1a;https://www.overleaf.com TeX Live下载&#xff1a;h…

使用 v-for 指令和数组来实现在 Uni-app 中动态增减表单项并渲染多个数据

在 data 中定义一个数组&#xff0c;用于存储表单项的数据&#xff1a; data() {return {formItems: []} } 在模板中使用 v-for 指令渲染表单项&#xff1a; <template><div><div v-for"(item, index) in formItems" :key"index"><…

【LeetCode】《LeetCode 101》第十二章:字符串

文章目录 12.1 字符串比较242 . 有效的字母异位词&#xff08;简单&#xff09;205. 同构字符串&#xff08;简单&#xff09;647. 回文子串&#xff08;中等&#xff09;696 . 计数二进制子串&#xff08;简单&#xff09; 12.2 字符串理解224. 基本计算器&#xff08;困难&am…

mysql之存储引擎

目录 存储引擎概念 MyISAM MyISAM特点 MyISAM 表的存储格式 MyISAM适用的生产场景 InnoDB InnoDB特点 选择存储引擎依据 MyISAM 和 INNODB区别 命令 查看系统支持的存储引擎 查看表使用的存储引擎 修改存储引擎 存储引擎概念 MySQL中的数据用各种不同的技术存…

通过python 获取当前局域网内存在的IP和MAC

通过python 获取当前局域网内存在的ip 通过ipconfig /all 命令获取局域网所在的网段 通过arp -d *命令清空当前所有的arp映射表 循环遍历当前网段所有可能的ip与其ping一遍建立arp映射表 for /L %i IN (1,1,254) DO ping -w 1 -n 1 192.168.3.%i 通过arp -a命令读取缓存的映射表…

Java的23种设计模式

Java的23种设计模式 一、创建型设计模式1.单例模式 singleton1.1.静态属性单例模式1.2 静态属性变种1.3 基础的懒汉模式1.4 线程安全的懒加载单例1.5 线程安全的懒加载 单例-改进1.6 双重检查锁1.7 静态内部类1.8 枚举单例1.9 注册表单例 2.工厂方法模式 factory3.抽象工厂模式…

通过chatgpt 学习React的useEffect

定义&#xff1a; useEffect 是 React 中的一个 Hook&#xff0c;它用于处理函数组件中的副作用操作。副作用操作可以包括数据获取、订阅事件、定时器等。 useEffect 接受两个参数&#xff1a;第一个参数是一个回调函数&#xff0c;用于执行副作用操作&#xff1b;第二个参数…

IE浏览器攻击:MS11-003_IE_CSS_IMPORT

目录 概述 利用过程 漏洞复现 概述 MS11-003_IE_CSS_IMPORT是指Microsoft Security Bulletin MS11-003中的一个安全漏洞&#xff0c;影响Internet Explorer&#xff08;IE&#xff09;浏览器。这个漏洞允许攻击者通过在CSS文件中使用import规则来加载外部CSS文件&#xff0…

SMU学习

SMU学习 1.参考资料 1.参考资料 TC3xx-SMU_EMS分析 英飞凌基础学习笔记&#xff08;SMU&#xff09;Safety Management Unit 为什么需要外部看门狗&#xff1f; ISO 26262 - Software Level of Functional Safety 简要概括下infeneon 芯片的SMU模块 ChatGPT 英飞凌&#xff0…

计算一个区间时间差值,时间保留剩下的差值

解决目的 begin end&#xff0c;去除集合类的其他区间差值List<rang> r1 new ArrayList(); 得到差值package com.jowoiot.wmzs.utils.date;import com.google.common.collect.Lists; import com.google.common.collect.Range; import org.apache.commons.lang.time.Dat…

51单片机项目(8)——基于51单片机的DS1302时钟系统

本次做的设计&#xff0c;是利用DS1302时钟芯片&#xff0c;做了一个时钟&#xff0c;并且将实时时间通过串口发送到上位机进行显示。系统运行如下&#xff1a;&#xff08;protues文件和相关keil代码会在文章最后给出&#xff01;&#xff01;&#xff01;&#xff09; DS1302…

[ES]二基础 |

一、索引库操作 1、mapping属性 mapping是对索引库中文档的约束&#xff0c;常见的mapping属性包括&#xff1a; 1)type&#xff1a;字段数据类型&#xff0c;常见的简单类型有&#xff1a; ①字符串&#xff1a;text(可分词的文本)、keyword&#xff08;精确值&#xff0c…

如何使用vue-smooth-dnd

Vue Smooth DnD是一个基于Vue的平滑易用的拖放库。它提供了简单易用的API和可自定义的样式。 要使用Vue Smooth DnD&#xff0c;可以按照以下步骤进行操作&#xff1a; 安装Vue Smooth DnD npm install vue-smooth-dnd --save 在组件中引入Vue Smooth DnD import VueSmoot…

【Leetcode】127.单词接龙(Hard)

一、题目 1、题目描述 字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列 beginWord -> s 1 s_1 s1​

【LeetCode每日一题】——274.H指数

文章目录 一【题目类别】二【题目难度】三【题目编号】四【题目描述】五【题目示例】六【题目提示】七【解题思路】八【时间频度】九【代码实现】十【提交结果】 一【题目类别】 排序 二【题目难度】 中等 三【题目编号】 274.H指数 四【题目描述】 给你一个整数数组 ci…