silverlight DataBind 小Demo

我其实不懂C#,原来学的java.最近因为毕业设计的缘故必须要学silverlight.属于按葫芦画瓢的级别.

DataBind的流程

1)在XAML中添加Binding Class.指定binding的源,目标和绑定模式.

示例{Binding FirstName,Mode=”oneway”}

2)在后置代码中添加事件和事件处理程序

3)书写类, 如果是twoway,实现INotifyProperty接口.添加NotifPropertyChanged事件.写处理程序,在构造函数中添加要通知的改变的属性

4)在代码中实例化绑定数据,然后将目标的DataContext设为被实例化的对象.

XAML

代码
<UserControl x:Class="DatabindDemo1.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:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Background="AliceBlue" Height="200" Margin="20,24,283,76">
<TextBlock Text="{Binding Price, Mode=OneWay}"
Name
="Price"></TextBlock>
<TextBlock Text="{Binding Title, Mode=OneWay}"
Name
="Title" Margin="100,77,100,223">
</TextBlock>
</StackPanel>
<Button Name="MyButton" Content="修改"
Click
="Button_Click" Width="40" Height="20" Margin="45,242,315,38">
</Button>
<TextBox Height="23" HorizontalAlignment="Left" Margin="210,26,0,0" Name="Header" VerticalAlignment="Top" Width="120" Text="{Binding Header,Mode=TwoWay}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="210,66,0,0" Name="ISBN" VerticalAlignment="Top" Width="120" Text="{Binding Isbn,Mode=TwoWay}" />
<TextBlock Height="20" HorizontalAlignment="Left" Margin="137,27,0,0" Name="textBlock1" Text="书名" VerticalAlignment="Top" Width="60" TextWrapping="NoWrap" FlowDirection="LeftToRight" Padding="20,0,0,0" />
<TextBlock Height="20" HorizontalAlignment="Left" Margin="137,67,0,0" Name="textBlock2" Text="ISBN" VerticalAlignment="Top" Width="60" TextWrapping="NoWrap" FlowDirection="LeftToRight" Padding="20,0,0,0" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="210,118,0,0" Name="Header_display" VerticalAlignment="Top" Width="120" Text="{Binding Header,Mode=TwoWay}"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="210,158,0,0" Name="ISBN_show" VerticalAlignment="Top" Width="120" Text="{Binding Isbn,Mode=TwoWay}"/>
<TextBlock FlowDirection="LeftToRight" Height="20" HorizontalAlignment="Left" Margin="137,119,0,0" Name="textBlock4" Padding="20,0,0,0" Text="书名" TextWrapping="NoWrap" VerticalAlignment="Top" Width="60" />
<TextBlock FlowDirection="LeftToRight" Height="20" HorizontalAlignment="Left" Margin="137,159,0,0" Name="textBlock5" Padding="20,0,0,0" Text="ISBN" TextWrapping="NoWrap" VerticalAlignment="Top" Width="60" />
</Grid>
</UserControl>

 

 

Book类

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;
using System.ComponentModel;

namespace DatabindDemo1
{
    public class Book :INotifyPropertyChanged
    {
        private string _title;
        public string Title {
            get { return _title; }
            set { _title = value; }
        }
        private double _price;
        public double Price {
            get { return _price; }
            set
            {
                _price = value;
                NotifyPropertyChange("Price");
            }
        }
        private string _header;
        public string Header {
            get { return _header; }
            set
            {
                _header = value;
                NotifyPropertyChange("Header");
            }
        }

        private string _isbn;
        public string Isbn
        {
            get { return _isbn; }
            set
            {
                _isbn = value;
                NotifyPropertyChange("Isbn");
            }
        }

        private void NotifyPropertyChange(string p)
        {
            if (PropertyChanged != null) {
                PropertyChanged(this,new PropertyChangedEventArgs(p));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

后置代码:

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;

namespace DatabindDemo1
{   
   
    public partial class MainPage : UserControl
    {  
        Book book = new Book();
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
            book.Title = "猜猜我有多爱你";
            book.Price = 40;
            Title.DataContext = book;
            Price.DataContext = book;
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Book b = new Book()
            {
            Header = "浮过生命海",
            Isbn = "8898098"
            };
            LayoutRoot.DataContext = b;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            book.Price = 50;
        }
    }
}

转载于:https://www.cnblogs.com/livewall/articles/1922733.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值