标签:des style blog http io color os ar 使用
<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> <!--分组转换器--> <local:PriceRangeProductGrouper x:Key="Price50Grouper" GroupInterval="100"></local:PriceRangeProductGrouper> <!--CollectionViewSource按价格分组:GroupInterval指定分组间隔--> <CollectionViewSource x:Key="GroupByRangeView"> <CollectionViewSource.SortDescriptions> <component:SortDescription PropertyName="UnitCost" Direction="Ascending"></component:SortDescription> </CollectionViewSource.SortDescriptions> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="UnitCost" Converter="{StaticResource Price50Grouper}"></PropertyGroupDescription> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Margin="7,7,7,0" Padding="2" Click="cmdGetProducts_Click">Get Products</Button> <!--Binding Source指定CollectionViewSource--> <ListBox Grid.Row="1" Margin="7,3,7,10" Name="lstProducts" ItemsSource="{Binding Source={StaticResource GroupByRangeView}}"> <!--项模板--> <ListBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock Text="{Binding ModelName}"></TextBlock> (<TextBlock Text="{Binding UnitCost,StringFormat={}{0:C}}"></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"></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.xaml 的交互逻辑 /// </summary> public partial class GroupInRanges : Window { public GroupInRanges() { InitializeComponent(); } private ICollection<Product> products; private void cmdGetProducts_Click(object sender, RoutedEventArgs e) { products = App.StoreDb.GetProducts(); //从xaml中查找CollectionViewSource CollectionViewSource viewSource = (CollectionViewSource)this.FindResource("GroupByRangeView"); //viewSource.Source指向数据集合ICollection<Product> products viewSource.Source = products; } } }
PriceRangeProductGrouper:价格转换器
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."); } } }
标签:des style blog http io color os ar 使用
原文地址:http://www.cnblogs.com/xiepengtest/p/4067074.html