第一个数据绑定的silverlight程序

原文链接: http://www.cnblogs.com/huangwj21/archive/2010/07/23/1783945.html

以往为了做wtt测试,曾经写过一个sample 的WPF application.对于silverlight的了解,仅限于是web 版的WPF。。。。。

关于silverlight,以前也做过一个sample,但仅仅是拖动一些控件到页面上,无任何其他功能。

今天做的这个silverlight程序,相当于把培训的内容重新复习了一遍,包含两个内容:

1. silverlight 简单数据绑定。

2.silverlight 4 新增的ICommand 接口的使用。

3.silverlight 中简单的automation 的实现。

关于topic1:

silverlight实现了view和model的分离,可以使页面设计者和程序员分开工作。如何实现呢?

silverlight控件例如 textblock,textbox,listbox等等,都可以和程序中的某个属性,进行绑定。

当程序员构建了一个class以后,在前端xaml文件的相应控件标签中加入 Text={Binding PropertyName}

例如:

public class sampleclass

{

  private string text = "Hello World";

  public string Text

  {

    get{return text;}

    set{text=value;}

  }

}

前端xaml

<TextBlock x:Name="HelloText" Text={Binding Text}>

这样就实现了将TextBlock中的Text和一个类中的属性进行绑定;

当然,还需要给TextBlock指定一个数据上下文datacontext(因为也许在你的众多类中,都有一个叫做Text的属性,那么程序怎么知道应该绑定哪个class或者哪个数据对象的Text呢,这里的datacontext就是起到这个作用)

可以在页面Intialize的时候指定this.DataContext = new sampleclass();

也可以在xaml文件中用resource标签中指定,还有很多其他的制定方法,这里暂不讨论;

这里还涉及到一个BindMode的问题,一共有三种OneTime,OneWay, TwoWay.

OneTime 指一次绑定,无论属性有没有发生变法,前台的现实都是不会改变的;

OneWay 指在绑定过程中,如果后台的属性发生改变,会直接在前台显示出来;

TwoWay 指在绑定过程中,前后台可以交互,相互影响;

在实现OneWay和TwoWay的时候,需要注意的是包含属性的对象,需要实现INotifyPropertyChanged

也就是通知系统属性的变化,具体代码:

 public class simpleclass:INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        private string text = "Hello World";
        private Command _saveCommand = null;

        public string Text
        {
            set { text = value; OnPropertyChanged("Text"); }
            get { return text; }
        }

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

关于topic2:

在silverlight以往的版本中,还无法实现对事件的直接绑定,在silverlight4中,可以实现对事件的绑定只要实现了ICommand接口,具体方法如下:

 public class Command : ICommand
    {
        public event EventHandler CanExecuteChanged;

        Predicate<Object> _canExecute = null;
        Action<Object> _excuteAction = null;

        public Command(Predicate<Object> canExecute, Action<object> excuteAction)
        {
            _canExecute = canExecute;
            _excuteAction = excuteAction;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute != null)
                return _canExecute(parameter);
            return true;
        }


        public void Execute(object parameter)
        {
            if (_excuteAction != null)
                _excuteAction(parameter);
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

    }

在simpleclass中加入方法

public class simpleclass: INotifyPropertyChanged

{

  ........

  private Command _saveCommand;

  //实现具体的方法

  public Command SaveCommand

  {

    get

    {

      if(_saveCommand == null)

          _saveCommand = new Command

        (

          p => true,

          p => Text = "Hello World Changed!!"

        );

      return _saveCommand;

    }

  }

}

xaml中

<TextBlock x:Name="HelloText" Text="{Binding Text}" Command = "{Binding SaveCommand}">

这样就实现了command的绑定

转载于:https://www.cnblogs.com/huangwj21/archive/2010/07/23/1783945.html

猜你喜欢

转载自blog.csdn.net/weixin_30603633/article/details/94963584
今日推荐