WPF 自定义范围分组
<Window x:Class="ViewExam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="437.165" Width="553.161" Loaded="Window_Loaded_1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Name="lstProducts" DisplayMemberPath="ModelName" Height="200" SelectionChanged="lstProducts_SelectionChanged_1">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White" Background="LightGreen" Margin="5,0,0,0" Padding="3"></TextBlock>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
<Grid Grid.Row="1" DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock>Model Number</TextBlock>
<TextBox Text="{Binding Path=ModelNumber}" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="1">Model Name</TextBlock>
<TextBox Text="{Binding Path=ModelName}" Grid.Column="1" Grid.Row="1"></TextBox>
<TextBlock Grid.Row="2">Unit Cost</TextBlock>
<TextBox Text="{Binding Path=UnitCost}" Grid.Column="1" Grid.Row="2"></TextBox>
<TextBlock Grid.Row="3">Description</TextBlock>
<TextBox Text="{Binding Path=Description}" TextWrapping="Wrap" Grid.Row="5" Grid.ColumnSpan="2"></TextBox>
</Grid>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button Name="btnPrevious" Click="btnPrevious_Click_1">previous</Button>
<Label x:Name="lblPosition" Width="400"></Label>
<Button Name="btnNext" Click="btnNext_Click_1">Next</Button>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<Label>Price than</Label>
<TextBox Name="txtMin" Width="200"></TextBox>
<Button Name="btnFilter" Click="btnFilter_Click_1">Filter</Button>
<Button Name="btnRemoveFilter" Margin="3,0,0,0" Click="btnRemoveFilter_Click_1">Remove Filter</Button>
</StackPanel>
</Grid>
</Window>
using DBAccess;
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;
namespace ViewExam
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private ListCollectionView view;
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
ICollection<Product> products = StoreDB.GetProducts();
lstProducts.ItemsSource = products;
this.DataContext = products;
view = (ListCollectionView)CollectionViewSource.GetDefaultView(products);
//view.Filter = new Predicate<object>(FilterProduct);
view.SortDescriptions.Add(new System.ComponentModel.SortDescription("CategoryID", System.ComponentModel.ListSortDirection.Ascending));
view.SortDescriptions.Add(new System.ComponentModel.SortDescription("UnitCost", System.ComponentModel.ListSortDirection.Ascending));
//view.GroupDescriptions.Add(new PropertyGroupDescription("CategoryID"));
PriceRangeProductGrouper grouper = new PriceRangeProductGrouper();
grouper.GroupInterval = 50;
view.GroupDescriptions.Add(new PropertyGroupDescription("UnitCost",grouper));
view.CurrentChanged += view_CurrentChanged;
view_CurrentChanged(this, null);
}
private bool FilterProduct(object obj)
{
Product pro = (Product)obj;
return pro.UnitCost > 100;
}
void view_CurrentChanged(object sender, EventArgs e)
{
lblPosition.Content = "Record " + (view.CurrentPosition + 1).ToString() + " of " + view.Count.ToString();
btnPrevious.IsEnabled = view.CurrentPosition > 0;
btnNext.IsEnabled = view.CurrentPosition < view.Count - 1;
}
private void btnPrevious_Click_1(object sender, RoutedEventArgs e)
{
view.MoveCurrentToPrevious();
}
private void btnNext_Click_1(object sender, RoutedEventArgs e)
{
view.MoveCurrentToNext();
}
ProductByPriceFilter filter;
private void btnFilter_Click_1(object sender, RoutedEventArgs e)
{
decimal min = Convert.ToDecimal(txtMin.Text);
filter = new ProductByPriceFilter(min);
view.Filter = filter.FilterItem;
}
private void btnRemoveFilter_Click_1(object sender, RoutedEventArgs e)
{
view.Filter = null;
}
private void lstProducts_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
view.MoveCurrentTo(lstProducts.SelectedItem);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ViewExam
{
public class PriceRangeProductGrouper:IValueConverter
{
public int GroupInterval { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
decimal price = (decimal)value;
if (price < GroupInterval)
{
return string.Format(culture, "Less than {0:C}", GroupInterval);
}
else
{
int interval = (int)price / GroupInterval;
int lowerLimit = interval * GroupInterval;
int upperLimit = (interval + 1) * GroupInterval;
return string.Format(culture, "{0:C} to {1:C}", lowerLimit, upperLimit);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using DBAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ViewExam
{
public class ProductByPriceFilter
{
public decimal MinimumPrice { get; set; }
public ProductByPriceFilter(decimal minimumPrice)
{
MinimumPrice = minimumPrice;
}
public bool FilterItem(object item)
{
Product pro = (Product)item;
if (pro!=null)
{
return pro.UnitCost > MinimumPrice;
}
return false;
}
}
}
WPF 自定义范围分组的更多相关文章
- WPF 自定义柱状图 BarChart
WPF 自定义柱状图 当前的Telerik控件.DevExpress控件在图表控件方面做得不错,但是有时项目中需要特定的样式,不是只通过修改图表的模板和样式就能实现的. 或者说,通过修改当前的第三方控 ...
- wpf 自定义圆形按钮
wpf 自定义圆形按钮 效果图 默认样式 获取焦点样式 点击样式 下面是实现代码: 一个是自定义控件类,一个是控件类皮肤 using System; using System.Collections. ...
- WPF自定义窗口基类
WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名 ...
- WPF 自定义 MessageBox (相对完善版)
WPF 自定义 MessageBox (相对完善版) 基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...
- WPF自定义Window样式(2)
1. 引言 在上一篇中,介绍了如何建立自定义窗体.接下来,我们需要考虑将该自定义窗体基类放到类库中去,只有放到类库中,我们才能在其他地方去方便的引用该基类. 2. 创建类库 接上一篇的项目,先添加一个 ...
- WPF自定义Window样式(1)
1. 引言 WPF是制作界面的一大利器.最近在做一个项目,用的就是WPF.既然使用了WPF了,那么理所当然的,需要自定义窗体样式.所使用的代码是在网上查到的,遗憾的是,整理完毕后,再找那篇帖子却怎么也 ...
- WPF自学入门(九)WPF自定义窗口基类
今天简单记录一个知识点:WPF自定义窗口基类,常用winform的人知道,winform的窗体继承是很好用的,写一个基础窗体,直接在后台代码改写继承窗体名.但如果是WPF要继承窗体,我个人感觉没有理解 ...
- WPF自定义TabControl样式
WPF自定义TabControl,TabControl美化 XAML代码: <TabControl x:Class="SunCreate.Common.Controls.TabCont ...
- WPF 自定义ComboBox样式,自定义多选控件
原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...
随机推荐
- catalina.out 和 catalina.log 的区别和用途
catalina.out catalina.out其实是tomcat的标准输出(stdout)和标准出错(stderr),这是在tomcat的启动脚本里指定的,如果没有修改的话stdout和stder ...
- CMakeListx.txt 编辑语法学习
已hello.cpp为源文件,构建一个CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(hello) add_executable( ...
- php实现 句子逆序(需求才是最好的老师)
php实现 句子逆序(需求才是最好的老师) 一.总结 一句话总结:需求才是最好的老师. 1.str_split()和explode()的区别? explode — 使用一个字符串分割另一个字符串 3 ...
- Android 用LinkedList实现队列
队列 队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在 ...
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 解决Request method 'GET' not supported问题
博主最近遇到了这个问题,解决情况如下 第一种情况:前台页面的表单在一些情况下没有指定POST方法: Ajax没有指定POST方法: 后台方法在一定情况下需要指定POST方法: 第二种情况:前端参数类型 ...
- Tricks(四十八)—— 注释一段代码
为 if 的条件判断表达式,传一个永假的语句,来注释一段代码: # Python if False: ... ... ... # C/C++ if (false) { ... ... } 永远不要直接 ...
- PPT之SmartArt功能
在PPT中,我们经常看到这样的漂亮的组合图标: 他们是怎么做出来的呢?其实用ppt自带的SmartArt功能就能做出来了. Tips:SmartArt可以直接先选择组合图标再填文字,还可以写好了文字, ...
- vijos1070 新年趣事之游戏 - 次小生成树
传送门 题目大意: 求原图的最小生成树,和次小生成树. 题目分析: kruskals求mst(\(O(mlogm)\)) 考虑次小生成树暴力的做法,因为次小生成树总是由最小生成树删掉一条边并添加一条边 ...
- 【9102】&&【a102】求a/b的高精度值
Time Limit: 10 second Memory Limit: 2 MB 问题描述 计算a/b的精度值,设a,b以一般整数输入,计算结果精确到小数后20位(结果四舍五入). Input 文件输 ...