Data binding using collections composed of mixed types of data. : Binding « Windows Presentation Foundation « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » BindingScreenshots 
Data binding using collections composed of mixed types of data.
Data binding using collections composed of mixed types of data.
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:WpfApplication1"
  x:Class="WpfApplication1.Window1"
  Title="CompositeCollections"
  SizeToContent="WidthAndHeight">
  <Window.Resources>
    <c:Employees x:Key="EmployeesData"/>

    <XmlDataProvider x:Key="NewStudentesData" XPath="NewStudentes/Student">
      <x:XData>
      <NewStudentes xmlns="">
        <Student Name="Jason" />
        <Student Name="Hercules" />
        <Student Name="Bellerophon" />
        <Student Name="Theseus" />
        <Student Name="Odysseus" />
        <Student Name="Perseus" />
      </NewStudentes>
      </x:XData>
    </XmlDataProvider>

    <DataTemplate DataType="{x:Type c:Employee}">
      <TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
    </DataTemplate>
    <DataTemplate DataType="Student">
      <TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
    </DataTemplate>
  </Window.Resources>
  
  <StackPanel>
    <ListBox Name="myListBox" Height="300" Width="200">
      <ListBox.ItemsSource>
        <CompositeCollection>
          <CollectionContainer Collection="{Binding Source={StaticResource EmployeesData}}" />
          <CollectionContainer Collection="{Binding Source={StaticResource NewStudentesData}}" />
          <ListBoxItem Foreground="Red">Other Listbox Item 1</ListBoxItem>
        </CompositeCollection>
      </ListBox.ItemsSource>
    </ListBox>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }
  }

  public class Employee
  {
        private string _name;

    public string Name
    {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
    }
        
        public Employee(string name)
        {
            Name = name;
        }
  }

  public class Employees : ObservableCollection<Employee>
  {
    public Employees()
    {
      Add(new Employee("A"));
      Add(new Employee("B"));
      Add(new Employee("C"));
    }
  }
}

   
    
  
Related examples in the same category
1.Bind property of one instantiated controlBind property of one instantiated control
2.Bind to Window itselfBind to Window itself
3.Two level path bindingTwo level path binding
4.Bind RelativeSource's AncestorTypeBind RelativeSource's AncestorType
5.Bind RelativeSource's AncestorType's PathBind RelativeSource's AncestorType's Path
6.Bind Stroke Thickness to SliderBind Stroke Thickness to Slider
7.Bind current time to ButtonBind current time to Button
8.Desktop to ControlDesktop to Control
9.Bind Label To ScrollBarBind Label To ScrollBar
10.Bind ScrollBar To LabelBind ScrollBar To Label
11.Binding With Data ContextBinding With Data Context
12.Long Binding PathLong Binding Path
13.Bind ListBox ItemsSource to DayNames property of DateTimeFormatInfoBind ListBox ItemsSource to DayNames property of DateTimeFormatInfo
14.Bind TextBlock Text to SelectedItem property of ListBoxBind TextBlock Text to SelectedItem property of ListBox
15.Bind To TextBox Back and ForthBind To TextBox Back and Forth
16.Binding FontFamily / FontSize value for current ControlBinding FontFamily / FontSize value for current Control
17.Bind to a Collection with the Master-Detail PatternBind to a Collection with the Master-Detail Pattern
18.Bind to IDataErrorInfo
19.Bind to a collectionBind to a collection
20.DataTemplate for bindingDataTemplate for binding
21.Binding Environment InfoBinding Environment Info
22.Bind to an ADO.NETDataSetBind to an ADO.NETDataSet
23.Add a value converter to a binding using XAMLAdd a value converter to a binding using XAML
24.Custom Dialog with data binding
25.One way and two way bindingOne way and two way binding
26.Object BindingObject Binding
27.Without BindingWithout Binding
28.Master Detail BindingMaster Detail Binding
29.Text Data BindingText Data Binding
30.List BindingList Binding
31.Async bindingAsync binding
32.Null property bindingNull property binding
33.Binding Property with ExceptionBinding Property with Exception
34.Hierarchical Binding for three level nested objectsHierarchical Binding for three level nested objects
35.BindingOperations.GetBindingExpressionBindingOperations.GetBindingExpression
36.Collection View Source BindingCollection View Source Binding
37.Bind to enum typesBind to enum types
38.Bind to a MethodBind to a Method
39.Bind an ItemsControl to the CollectionViewSource, Set its DisplayMemberPath to display the Name propertyBind an ItemsControl to the CollectionViewSource, Set its DisplayMemberPath to display the Name property
40.Bind to the Values of an EnumerationBind to the Values of an Enumeration
41.Binding Dependency Property to TextBlockBinding Dependency Property to TextBlock
42.Bind Your Objects to UI Control with PropertyBind Your Objects to UI Control with Property
43.Implement INotifyPropertyChanged to notify the binding targets when the values of properties change.Implement INotifyPropertyChanged to notify the binding targets when the values of properties change.
44.Bind to an Existing Object InstanceBind to an Existing Object Instance
45.Digital ClockDigital Clock
46.Formatted Digital ClockFormatted Digital Clock
47.Manual Update TargetManual Update Target
48.Property changed callbackProperty changed callback
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.