SilverLight学习笔记--对象数据绑定
对象数据绑定
分三步实现:
1:在工程中创建一个类
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication6
{
public class Book
{
public Book() { }
public Book(string isbn, string title,
DateTime publishdate, double price)
{
this.ISBN = isbn;
this.Title = title;
this.PublishDate = publishdate;
this.Price = price;
}
//Define the public properties
public string ISBN { get; set; }
public string Title { get; set; }
public DateTime PublishDate { get; set; }
public double Price { get; set; }
}
}
2:编写Xaml代码
<UserControl x:Class="SilverlightApplication6.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Orientation="Horizontal">
<ListBox x:Name="MyBooks" Margin="5" ItemsSource="{Binding Mode=OneWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding ISBN}" Margin="0,0,50,0" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel>
<!--Visual division between the list and the details-->
<Rectangle HorizontalAlignment="Left" Width="400" Height="2" Fill="Red" Margin="0,10,0,10"/>
<!--The UI for the details view-->
<StackPanel x:Name="BookDetails">
<TextBlock Text="{Binding ISBN, Mode=OneWay}" />
<TextBlock Text="{Binding Title, Mode=OneWay}" />
<TextBlock Text="{Binding PublishDate, Mode=OneWay }" />
<TextBlock Text="{Binding Price, Mode=OneWay}" />
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
3:编写Page.xaml.cs
打开 Page.xaml.cs
注意添加引用 using System.Windows.Shapes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace SilverlightApplication6
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
AllBooks.Add(new Book("4458907683", "Training Your Dog", new DateTime(2000, 2, 8), 44.25));
AllBooks.Add(new Book("0446675385", "Good Owners, Great Dogs", new DateTime(1999, 9, 1), 15.99));
//Set the data context for the list of books
MyBooks.DataContext = AllBooks;
this.MyBooks.SelectionChanged += new SelectionChangedEventHandler(MyBooks_SelectionChanged);
MyBooks.SelectedIndex = 0;
}
private void MyBooks_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox myBooks = sender as ListBox;
BookDetails.DataContext = myBooks.SelectedItem;
}
private ObservableCollection<Book> _AllBooks;
public ObservableCollection<Book> AllBooks
{
get
{
if (_AllBooks == null)
{
_AllBooks = new ObservableCollection<Book>();
_AllBooks.Add(new Book("3390092284", "All About Dogs",
new DateTime(2004, 3, 4), 12.99));
}
return _AllBooks;
}
}
}
}
目前维护的开源产品:https://gitee.com/475660