zoukankan      html  css  js  c++  java
  • .Silverlight Binding (One Time,One Way,Two Way)

    本文将探讨Silverlight绑定的三种方式-One time,One way,Two way。

      One way binding

      从名称就可以了解到该方法只支持单向的从数据对象到UI的绑定。例如,界面上有一个textbox name='txtYear',text属性绑定了某个数据对象的'year'属性。

    则一旦该数据对象发生了改变,txtYear的text会跟随改变。但是相反的,txtYear的text属性的改变却不会影响到数据对象。

    <TextBox x:Name="txtYear" Text="{Binding Year, Mode=OneWay}"></TextBox>

    下面是绑定的方法。

     private void bind()
    {   
    DateClass myDate
    = new DateClass ();
    myDate .Year
    = DateTime.Now.Year;   
    txtYear.DataContext
    = myDate ;
    }

      

      Two way binding

      Two way binding模式下,数据在数据对象和UI之间是同步的,一旦任何一方发生了改变,都会同时改变另一方。

      

    <TextBox x:Name="txtYear" Text="{Binding Year, Mode=TwoWay}" ></TextBox>

      需要注意的是,如果是自定义的数据对象,我们需要引用System.ComponentModel,继承INotifyPropertyChanged接口

    并在该对象中实现PropertyChanged。

    Silverlight Binding (One Time,One Way,Two Way) - dingtao-wgs - 程序员驿站代码
    public class DateClass : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    private int _intYear;
    public int Year{
    set
    {  
    _intYear
    = value;  
    OnPropertyChanged(
    "Year");
    }
    get
    {  
    return _intYear;
    }
    }

    private void OnPropertyChanged(string property)
    {
    if (PropertyChanged != null)
    {  
    PropertyChanged(
    this,new PropertyChangedEventArgs(property));
    }
    }
    }

       与UI的绑定方法同上。

      One time binding

      One time binding模式下,数据仅仅与UI绑定一次,数据对象与UI任何一方发生改变都不会影响到另一方面,One time binding在效率上比前两种高,比较适用于报表之类只需要绑定一次数据的UI对象。

    <TextBox x:Name="txtYear" Text="{Binding Year, Mode=OneTime}" ></TextBox>

      如上所述,Silverlight的三种绑定方式就是如此,至于什么时候用哪种方式就取决当时的情况了。

  • 相关阅读:
    系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式(分享二十二)
    某云数据中心网络解决方案(分享二十一)
    oracle 12c 管理(分享二十)
    Codeforces 356D 倍增优化背包
    Codeforces 360D Levko and Sets (数论好题)
    gym/102253C Colorful Tree 树上计数
    Codeforces 360E 贪心 最短路
    Codeforces 360C DP 计算贡献
    Codeforces 354B 博弈, DP,记忆化搜索
    Codeforces 354C 暴力 数论
  • 原文地址:https://www.cnblogs.com/suncarry/p/2072609.html
Copyright © 2011-2022 走看看