<Window x:Class="DataBinding.GroupInRanges"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:component="clr-namespace:System.ComponentModel;assembly=WindowsBase"
         xmlns:local="clr-namespace:DataBinding"
    Title="GroupInRanges" Height="300" Width="300"
    >

  <Window.Resources>
    <local:PriceRangeProductGrouper x:Key="Price50Grouper" GroupInterval="50"/>
    <CollectionViewSource x:Key="GroupByRangeView">
      <CollectionViewSource.SortDescriptions>
        <component:SortDescription PropertyName="UnitCost" Direction="Ascending"/>        
      </CollectionViewSource.SortDescriptions>
      <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="UnitCost" Converter="{StaticResource Price50Grouper}"/>
      </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>
    <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"/>
            </DataTemplate>
          </GroupStyle.HeaderTemplate>
        </GroupStyle>
      </ListBox.GroupStyle>

    </ListBox>    
  </Grid>
</Window>
