<Window X: class = "demo. groupinranges "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Title =" groupinranges "Height =" 300 "width =" 300 "xmlns: Local =" CLR-namespace: demo "xmlns: Component =" CLR-namespace: system. componentmodel; Assembly = windowsbase "> <window. resources> <! -- Group converter --> <local: pricerangeproductgrouper X: Key = "price50grouper" groupinterval = "100"> </local: pricerangeproductgrouper> <! -- Collectionviewsource: groupinterval specifies the Group Interval --> <collectionviewsource X: Key = "groupbyrangeview"> <collectionviewsource. sortdescriptions> <component: sortdescription propertyname = "unitcost" ction = "ASCENDING"> </component: sortdescription> </collectionviewsource. sortdescriptions> <collectionviewsource. groupdescriptions> <propertygroupdescription propertyname = "unitcost" converter = "{staticresource P Rice50grouper} "> </propertygroupdescription> </collectionviewsource. groupdescriptions> </collectionviewsource> </window. resources> <grid. rowdefinitions> <rowdefinition Height = "Auto"> </rowdefinition> </grid. rowdefinitions> <button margin = "7,7, 7,0" padding = "2" Click = "acknowledgetproducts_click"> get products </button> <! -- Binding source specifies collectionviewsource --> <ListBox grid. row = "1" margin = "7,3, 7,10" name = "lstproducts" itemssource = "{binding source = {staticresource groupbyrangeview}"> <! -- Item template --> <ListBox. itemtemplate> <datatemplate> <textblock text = "{binding modelname}"> </textblock> (<textblock text = "{binding unitcost, stringformat = {} {0: c }}"> </textblock>) </textblock> </datatemplate> </ListBox. itemtemplate> <! -- Group style --> <ListBox. groupstyle> <groupstyle. headertemplate> <datatemplate> <textblock text = "{binding Path = Name}" fontweight = "bold" foreground = "white" background = "lightgreen" margin = "0, 5, 0, 0 "padding =" 3 "> </textblock> </datatemplate> </groupstyle. headertemplate> </groupstyle> </ListBox. groupstyle> </ListBox> </GRID> </WINDOW>
Using system; using system. LINQ; using system. text; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. imaging; using system. windows. shapes; using storedatabase; using system. collections. generic; using system. windows. data; namespace demo {// <summary> // groupinranges. interaction logic of XAML // </Summary> Public partial class groupinranges: window {public groupinranges () {initializecomponent ();} private icollection <product> products; private void initialize getproducts_click (Object sender, routedeventargs e) {products = app. storedb. getproducts (); // search for collectionviewsource viewsource = (collectionviewsource) This from XAML. findresource ("groupbyrangeview"); // viewsource. source points to the data collection icollection <product> Products viewsource. source = products ;}}}
Pricerangeproductgrouper: price Converter
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;using System.Globalization;namespace demo{ 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 uperLimit = (interval + 1) * GroupInterval; return String.Format("{0:C} to {1:C}",lowerLimit,uperLimit); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException("This converter is for grouping only."); } }}
D21_04view _ use range grouping (use value converter)