Display Bounded objects onto ListBox : ListBox Data « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplication1"
  xmlns:compModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework" 
  Title="CollectionViewSourceBinding" Height="300" Width="300">
  <Window.Resources>
    <local:People x:Key="Family">
      <local:Employee Name="A" Age="11" />
      <local:Employee Name="B" Age="12" />
      <local:Employee Name="C" Age="28" />
      <local:Employee Name="D" Age="88" />
    </local:People>

    <local:AgeToRangeConverter x:Key="ageConverter" />
    
    <CollectionViewSource x:Key="SortedGroupedFamily" Source="{StaticResource Family}">
      
      <CollectionViewSource.SortDescriptions>
        <compModel:SortDescription PropertyName="Name" Direction="Ascending" />
        <compModel:SortDescription PropertyName="Age" Direction="Descending" />
      </CollectionViewSource.SortDescriptions>
      
      <CollectionViewSource.GroupDescriptions>
        <data:PropertyGroupDescription PropertyName="Age" Converter="{StaticResource ageConverter}" />
        <data:PropertyGroupDescription PropertyName="Age" />
      </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

  </Window.Resources>
  <Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource SortedGroupedFamily}}" DisplayMemberPath="Name">
      <ListBox.GroupStyle>
        <x:Static Member="GroupStyle.Default" />
      </ListBox.GroupStyle>
    </ListBox>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;


namespace WpfApplication1 {

  public class Employee : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propName) {
      if( this.PropertyChanged != null ) {
        PropertyChanged(this, new PropertyChangedEventArgs(propName));
      }
    }

    string name;
    public string Name {
      get { return this.name; }
      set {
        if( this.name == value ) { return; }
        this.name = value;
        Notify("Name");
      }
    }

    int age;
    public int Age {
      get { return this.age; }
      set {
        if( this.age == value ) { return; }
        this.age = value;
        Notify("Age");
      }
    }

    public Employee() { }
    public Employee(string name, int age) {
      this.name = name;
      this.age = age;
    }
  }

  class People : ObservableCollection<Employee> { }

  public partial class Window1 : System.Windows.Window {
    public Window1() {
      InitializeComponent();
    }

  }

  public class AgeToRangeConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return (int)value < 25 ? "Under 25" : "Over 25";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

      throw new NotImplementedException();
    }
  }

}
WPF Display Bounded Objects Onto List Box








24.32.ListBox Data
24.32.1.Convert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverterConvert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverter
24.32.2.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.
24.32.3.Display Bounded objects onto ListBoxDisplay Bounded objects onto ListBox
24.32.4.Use DataTemplate in ListBoxUse DataTemplate in ListBox
24.32.5.ListBox binds to the people collection, and sets the DataTemplate to use for displaying each itemListBox binds to the people collection, and sets the DataTemplate to use for displaying each item
24.32.6.Without specifying a DataTemplate, the ListBox displays a list of names.Without specifying a DataTemplate, the ListBox displays a list of names.
24.32.7.Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverterConvert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter
24.32.8.Convert contents of a ListBoxItem to an instance of Thickness by using the BrushConverterConvert contents of a ListBoxItem to an instance of Thickness by using the BrushConverter
24.32.9.DataTrigger, ListBox and user objectDataTrigger, ListBox and user object
24.32.10.Select Product Page FunctionSelect Product Page Function
24.32.11.Create ListBox and Add string as the ListBoxItem
24.32.12.ListBox of Color Values
24.32.13.ListBox of Color Shapes