数値でグループ化する例を考えてみます。少数の切りのいい整数のリストがない場合は、一意の値を集めると、管理不能な大きさになることがあります。このような場合は、代わりに、「0 〜 100」、「5000 以上」などのいくつかの範囲に項目をグループ化するカスタムグループ化アクションを適用できます。それには、IValueConverter を PropertyGroupDescription パラメータに渡してカスタムグループ化を実行します。
たとえば、次のコードは、各グループをアルファベット1文字(Countries: A、Countries: B、Countries: C など)としてリストして、顧客コレクションを国に基づいてグループ化します。Countries: A グループには、アルジェリア、アルゼンチン、オーストリアに属するすべての項目が含まれます。前述のコードスニペットを次のように変更します
Visual Basic コードの書き方
Visual Basic |
コードのコピー
|
---|---|
_view.GroupDescriptions.Add(New C1.Xaml.PropertyGroupDescription("Country", New GroupByCountryAtoZConverter())) |
C#コードの書き方
C# |
コードのコピー
|
---|---|
_view.GroupDescriptions.Add(new C1.Xaml.PropertyGroupDescription("Country", new GroupByCountryAtoZConverter())); |
さらに、次の GroupByCountryAtoZConverter クラスをプロジェクトに追加します。
Visual Basic コードの書き方
Visual Basic |
コードのコピー
|
---|---|
Public Class GroupByCountryAtoZConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As String) As Object If value IsNot Nothing Then Return value.ToString()(0) End If Return "Undefined" End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As String) As Object Throw New NotImplementedException() End Function End Class |
C# コードの書き方
C# |
コードのコピー
|
---|---|
public class GroupByCountryAtoZConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string culture) { if (value != null) { return value.ToString()[0]; } return "Undefined"; } public object ConvertBack(object value, Type targetType, object parameter, string culture) { throw new NotImplementedException(); } } |