This project is read-only.

CountConverter

Count converter finds the count of elements in a collection during data binding using specified property. The converter can count distinct or all property of elements within nested collections. Nested levels are specified using a CountParameter object and passed as ConverterParameter duding binding.

Namespace

StoreAppLib.Converters

Using CountConverter

To use CountConverter in your project, add the reference to StoreAppLib project or  install StoreAppLib from Visual Studio "Manage NuGet Packages" tool.

To use the converter while binding first declare the converter and count parameter in the App.xaml file.

<Application
   xmlns:storeAppConverter="using:StoreAppLib.Converters"
   ... >
   <Application.Resources>
       <ResourceDictionary>

           <storeAppConverter:CountConverter
               
x:Key="CountConverter" />   

           <storeAppConverter:CountParameter
               
x:Key="LanguageCountParameter" 
                Property
="Employees.Languages.Name" 
               
Distinct="False"
/>
 

          
<storeAppConverter:CountParameter
               
x:Key="DistinctLanguageCountParameter" 
                Property
="Employees.Languages.Name" 
               
Distinct="True"
/>
   

        </ResourceDictionary>
   </Application.Resources>
</
Application>

The count parameter declaration in the above code can be used to concatenate languages of all employees in all departments. LanguageCountParameter finds all count of languages whereas DistinctLanguageCountParameter finds distinct count of languages.

Following code displays total language count while binding. 

<TextBlock Text="{Binding Company.Departments,
           Converter
={StaticResource CountConverter},
           ConverterParameter
={StaticResource LanguageConcatenationParameter}}"
           Style
="{StaticResource BasicTextStyle}" />

Following code displays distinct language count while binding. 

<TextBlock Text="{Binding Company.Departments,
           Converter
={StaticResource CountConverter},
           ConverterParameter
={StaticResource DistinctLanguageConcatenationParameter}}"
           Style
="{StaticResource BasicTextStyle}" />

Last edited Nov 28, 2012 at 1:45 AM by sureshkumart, version 2

Comments

No comments yet.