Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello very body…
I want to learn how can i bind two TextBlocks to a ListBox as source in wpf that my ListBox shows a list of persons and one of two TextBlocks shows count of Male persons and another one shows count of Female persons without any codebehind coding (I want to bind my TextBlocks to ListBox directly in xaml code).
Thanks a lot my dears….!
Clifford Nelson 13-Jul-12 12:49pm    
When you talk about no code behind, are you looking at an MVVM solution. XAML is somewhat limited in do any sort of calculation, so sum/count would be a little beyond it. I would like it if Microsoft had continued to develope XAML since I think it could do so much more.

1 solution

The best option is to create a ValueConverter (or two value converters). You did not give enough detail to provide exactly the right answer, but here I am returning a count of where a ListBox is bound to an ItemsSource of IEnumerable<string>, and counting matches where the string is the same as the parameter:

public class CountConverter : IValueConverter
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    var listBox = value as ListBox;
    if (listBox != null)
      var list = listBox.ItemsSource as IEnumerable<string>;
      if (list != null)
        return list.Count(i => i == parameter.ToString());
    return null;

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    throw new NotImplementedException();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900