listbox groupage

main
syneffort 2 years ago
parent 931cf161b4
commit e58dd72f90
  1. 27
      PacticeSolution/ListSample/Converter/AgeToRangeConverter.cs
  2. 1
      PacticeSolution/ListSample/ListSample.csproj
  3. 29
      PacticeSolution/ListSample/MainWindow.xaml
  4. 13
      PacticeSolution/ListSample/MainWindow.xaml.cs

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ListSample.Converter
{
[ValueConversion(typeof(int), typeof(string))]
internal class AgeToRangeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
return (int)value >= 40 ? "OB Group" : "YB Group";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}

@ -55,6 +55,7 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Converter\AgeToRangeConverter.cs" />
<Compile Include="Resources.Designer.cs"> <Compile Include="Resources.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>

@ -33,7 +33,7 @@
<TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" <TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"
Text="Name: "/> Text="Name: "/>
<TextBlock Grid.Column="2" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" <TextBlock Grid.Column="2" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"
FontSize="18" FontWeight="Bold" FontWeight="Bold"
Text="{Binding Name}"/> Text="{Binding Name}"/>
<Button x:Name="btnDelete" Grid.Column="3" Grid.Row="1" Margin="3" <Button x:Name="btnDelete" Grid.Column="3" Grid.Row="1" Margin="3"
Content="Delete" Content="Delete"
@ -54,8 +54,31 @@
</Window.Resources> </Window.Resources>
<Grid> <Grid>
<ListBox x:Name="lbStudentList" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Stretch" <Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox x:Name="lbStudentList" Grid.Row="0" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Stretch"
ItemTemplate="{StaticResource studentTemplate}" ItemTemplate="{StaticResource studentTemplate}"
ItemsSource="{Binding}"/> ItemsSource="{Binding}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Background="Black" Foreground="White" FontSize="18" FontWeight="Bold" HorizontalAlignment="Center">
<TextBlock Text="{Binding Name}"/> (<TextBlock Text="{Binding ItemCount}"/>Person)
</TextBlock>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
<StackPanel Grid.Row="1">
<Button x:Name="btnGroup"
Content="Gourpage"
Click="btnGroup_Click"/>
</StackPanel>
</Grid> </Grid>
</Window> </Window>

@ -1,6 +1,8 @@
using ListSample.Model; using ListSample.Converter;
using ListSample.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -38,5 +40,14 @@ namespace ListSample
Student student = (Student)button.DataContext; Student student = (Student)button.DataContext;
_studentList.Remove(student); _studentList.Remove(student);
} }
private void btnGroup_Click(object sender, RoutedEventArgs e)
{
ICollectionView list = CollectionViewSource.GetDefaultView(_studentList);
if (list.GroupDescriptions.Count == 0)
list.GroupDescriptions.Add(new PropertyGroupDescription("Age", new AgeToRangeConverter()));
else
list.GroupDescriptions.Clear();
}
} }
} }

Loading…
Cancel
Save