• 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;

                }

            }

     

        }

    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    Celery
    mysql 8.0.12 创建并授权出现的问题
    request对象
    Haystack搜索框架
    Django的缓存机制
    跨域问题
    解析器
    url控制器与响应器
    学期总结
    C语言I博客作业09
  • 原文地址:https://www.cnblogs.com/starcrm/p/1351258.html
Copyright © 2020-2023  润新知