说明:
本例子中的TextBox和ComboBox绑定的是一个数据集合, ObservableCollectioin来自命名空间System.Collections.ObjectModel。
VB:Public MyBooks As New ObservableCollection(Of Book)()
C#: Public ObservableCollection <Book> MyBooks=New ObservableCollection<Book>();
待绑定的数据<TextBox x:Name="txtbox1" Text="{Binding Mode=OneWay}"/>
MainPage.xaml:
<UserControl x:Class="singleitem.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" ShowGridLines="True">
<Grid.RowDefinitions >
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="140"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="140"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<!--textbox1 Binding a control to a single Item-->
<TextBox x:Name="textbox1" Grid.Row="1" Text="{Binding Mode=OneWay}"></TextBox>
<!--combobox1 Binding a control to a collection of Objects-->
<ComboBox x:Name="combobox1" Grid.Row="2" Height="40" Width="300" ItemsSource="{Binding Mode=OneWay}" HorizontalAlignment="left"></ComboBox>
<!--combobox2 display items in a control by using a Data Template-->
<ComboBox x:Name="combobox2" Grid.Row="3" Height="40" Width="300" ItemsSource="{Binding Mode=OneWay}" HorizontalAlignment="left">
<ComboBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Author:" Margin="2"/>
<TextBlock Text="{Binding AuthorName}" Margin="2"/>
<TextBlock Text="Book:" Margin="10,2,0,2"/>
<TextBlock Text="{Binding BookName}" Margin="2"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<!--combobox3 adding a details view for selected item in combobox-->
<ComboBox x:Name="combobox3" Grid.Row="4" Height="40" Width="300" ItemsSource="{Binding Mode=OneWay}" HorizontalAlignment="left" SelectionChanged="combobox3_SelectionChanged" VerticalAlignment="Top">
<ComboBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal" Margin="2" >
<TextBlock Text="Author:" Margin="2" />
<TextBlock Text="{Binding AuthorName}" Margin="2"/>
<TextBlock Text="Book:" Margin="10,2,0,2"/>
<TextBlock Text="{Binding BookName}" Margin="2"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<StackPanel Grid.Row="4" x:Name="DetailPanel" VerticalAlignment="Bottom">
<!--AuthorName is the property Name in the Book Class-->
<!--BookName is the property Name in the Book Class-->
<!--ReleaseTime is the property Name in the Book Class-->
<TextBlock Text="{Binding AuthorName, Mode=OneWay}"/>
<TextBlock Text="{Binding BookName, Mode=OneWay}"/>
<TextBlock Text="{Binding ReleaseTime, Mode=OneWay}"/>
</StackPanel>
</Grid>
</UserControl>
MainPage.xaml.vb:
Imports System.Collections.ObjectModel
Partial Public Class MainPage
Inherits UserControl
'ObservableCollection defined in System.Collection.ObjectModel
Public MyBooks As New ObservableCollection(Of Book)()
Public Sub New()
InitializeComponent()
End Sub
Private Sub LayoutRoot_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
textbox1.DataContext = New Book("Cooper", "Simple Life", System.DateTime.Now)
MyBooks.Add(New Book("Cooper", "Cooper Notes", New DateTime(2007, 2, 17)))
MyBooks.Add(New Book("Cooper", "Graduation", New DateTime(2008, 2, 17)))
MyBooks.Add(New Book("Sweety", "Sweety's Diary", New DateTime(2009, 2, 17)))
combobox1.DataContext = MyBooks
combobox2.DataContext = MyBooks
combobox3.DataContext = MyBooks
End Sub
Private Sub combobox3_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
Dim mys As ComboBox = TryCast(sender, ComboBox)
DetailPanel.DataContext = MyBooks(mys.SelectedIndex)
End Sub
End Class
''' <summary>
''' Book Class as Object
''' </summary>
''' <remarks></remarks>
Public Class Book
Public Sub New()
End Sub
Public Sub New(ByVal author As String, ByVal book As String, ByVal release As DateTime)
AuthorName = author
BookName = book
ReleaseTime = release
End Sub
Private _authorName As String
Public Property AuthorName() As String
Get
Return _authorName
End Get
Set(ByVal value As String)
_authorName = value
End Set
End Property
Private _bookName As String
Public Property BookName() As String
Get
Return _bookName
End Get
Set(ByVal value As String)
_bookName = value
End Set
End Property
Private _releaseTime As DateTime
Public Property ReleaseTime() As DateTime
Get
Return _releaseTime
End Get
Set(ByVal value As DateTime)
_releaseTime = value
End Set
End Property
Public Overrides Function ToString() As String
Return BookName + " by " + AuthorName + ", Published: " + ReleaseTime.ToShortDateString()
End Function
End Class