[Html]
<Window x: Class = "TestOfBindingItemsControl. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525">
<StackPanel x: Name = "stackPanel" Background = "LightBlue">
<TextBlock Text = "StudentID:" Margin = "5"/>
<TextBox x: Name = "textBoxId" Margin = "5"/>
<TextBlock Text = "StudentList:" FontWeight = "Bold"
Margin = "5"/>
<ListBox x: Name = "listBoxStudents"
Height = "110"
Margin = "5"
>
<ListBox. ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "{Binding Path = Id}" Width = "30"/>
<TextBlock Text = "{Binding Path = Name}" Width = "60"/>
<TextBlock Text = "{Binding Path = Age}" Width = "30"/>
</StackPanel>
</DataTemplate>
</ListBox. ItemTemplate>
</ListBox>
</StackPanel>
</Window>
<Window x: Class = "TestOfBindingItemsControl. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525">
<StackPanel x: Name = "stackPanel" Background = "LightBlue">
<TextBlock Text = "StudentID:" Margin = "5"/>
<TextBox x: Name = "textBoxId" Margin = "5"/>
<TextBlock Text = "StudentList:" FontWeight = "Bold"
Margin = "5"/>
<ListBox x: Name = "listBoxStudents"
Height = "110"
Margin = "5"
>
<ListBox. ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "{Binding Path = Id}" Width = "30"/>
<TextBlock Text = "{Binding Path = Name}" Width = "60"/>
<TextBlock Text = "{Binding Path = Age}" Width = "30"/>
</StackPanel>
</DataTemplate>
</ListBox. ItemTemplate>
</ListBox>
</StackPanel>
</Window>
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
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. Navigation;
Using System. Windows. Shapes;
Namespace TestOfBindingItemsControl
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
List <Student> stuList = new List <Student> ()
{
New Student () {Id = 0, Name = "Tim", Age = 29 },
New Student () {Id = 1, Name = "Tom", Age = 28 },
New Student () {Id = 2, Name = "Kyle", Age = 27 },
New Student () {Id = 3, Name = "Tony", Age = 26 },
New Student () {Id = 4, Name = "Vine", Age = 25 },
New Student () {Id = 5, Name = "Mike", Age = 24}
};
// Set Binding for ListBox
This. listBoxStudents. ItemsSource = stuList;
// This. listBoxStudents. DisplayMemberPath = "Name ";
Binding binding = new Binding ("SelectedItem. Id ")
{
Source = this. listBoxStudents
};
This. textBoxId. SetBinding (TextBox. TextProperty, binding );
}
}
Public class Student
{
Public int Id {get; set ;}
Public string Name {get; set ;}
Public int Age {get; set ;}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
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. Navigation;
Using System. Windows. Shapes;
Namespace TestOfBindingItemsControl
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
List <Student> stuList = new List <Student> ()
{
New Student () {Id = 0, Name = "Tim", Age = 29 },
New Student () {Id = 1, Name = "Tom", Age = 28 },
New Student () {Id = 2, Name = "Kyle", Age = 27 },
New Student () {Id = 3, Name = "Tony", Age = 26 },
New Student () {Id = 4, Name = "Vine", Age = 25 },
New Student () {Id = 5, Name = "Mike", Age = 24}
};
// Set Binding for ListBox
This. listBoxStudents. ItemsSource = stuList;
// This. listBoxStudents. DisplayMemberPath = "Name ";
Binding binding = new Binding ("SelectedItem. Id ")
{
Source = this. listBoxStudents
};
This. textBoxId. SetBinding (TextBox. TextProperty, binding );
}
}
Public class Student
{
Public int Id {get; set ;}
Public string Name {get; set ;}
Public int Age {get; set ;}
}
}