数据绑定

  数据绑定是一种历经时间考验的传统方式,做法是从对象提取信息,并在应用程序的用户界面中显示提取信息,不用编写枯燥的代码就可以完成所有功能。富客户端通常使用双向数据绑定,这种数据绑定提供了从用户界面向一些对象退出信息的能力—同样,不需要或者几乎不需要编写代码。

使用自定义对象绑定到数据库

首先定义一个Book类

class Book
    {

        public Book(int bookID,string bookName,string press,string description)
        {
            _bookID = bookID;
            _bookName = bookName;
            _press = press;
            _description = description;
        }

        private int _bookID=0;

        public int BookID
        {
            get
            {
                return _bookID;
            }

            set
            {
                _bookID = value;
            }
        }
            
        private string _bookName=string.Empty;
        public string BookName
        {
            get
            {
                return _bookName;
            }

            set
            {
                _bookName = value;
            }
        }

        private string _press= string.Empty;
        public string Press
        {
            get
            {
                return _press;
            }

            set
            {
                _press = value;
            }
        }

        private string _description= string.Empty;
        public string Description
        {
            get
            {
                return _description;
            }

            set
            {
                _description = value;
            }
        }

    }

 

  前台代码:

<Window x:Class="WpfApplication3.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="gridBookDetails">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Margin="7">Book ID:</TextBlock>
        <TextBox Margin="5" Grid.Column="1" Text="{Binding Path=BookID}"></TextBox>
        <TextBlock Margin="7" Grid.Row="1">书名:</TextBlock>
        <TextBox Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding Path=BookName}"></TextBox>
        <TextBlock Margin="7" Grid.Row="2">出版社:</TextBlock>
        <TextBox Margin="5" Grid.Row="2" Grid.Column="2" Text="{Binding Path=Press}"></TextBox>
        <TextBlock Margin="7" Grid.Row="3">描述:</TextBlock>
        <TextBox Margin="5" Grid.Row ="4" Grid.ColumnSpan="2" TextWrapping="Wrap" Text="{Binding Path=Description}"/>
    </Grid>
</Window>

后台代码:

        public MainWindow()
        {
            InitializeComponent();
            Book book = new Book(151201,"JavaScript高级编程","人民邮电出版社","前端开发人员必备");
            gridBookDetails.DataContext = book;
        }

Grid控件设置DataContext属性,使用book实例内容数据填充Grid

运行界面如下:

posted @ 2016-01-03 19:47  幽弥狂233  阅读(156)  评论(0编辑  收藏  举报