Silverlight使用笔记

1. ItemsSource绑定写法,此写法可为一个页面绑定多个对象
  ItemsSource
="{Binding QuoteItems, Source={StaticResource QIViewModel}}"

2. Style需合并到App.xaml
  Style合并
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/Styles.xaml"/>
                <ResourceDictionary Source="Style.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

3. INotifyPropertyChanged
  
虽然看过INotifyPropertyChanged的相关资料,但其实一直没搞清楚他的真正作用,做了个测试,请看下面的代码,Product类有Id和Name属性。
  [1] Name属性注释掉RaisePropertyChanged(new PropertyChangedEventArgs("Name")),可以得到Name的值,这点说明只要类的属性与控件binding,那么就可以获取到在页面输入的值。
  [2] 我们在页面放置两个控件:textbox,textblock,并将text都绑定为product的name属性,在textbox中输入的name值没有反应到textblock上的,
去掉
注释,textblock的文本跟随改变,这说明INotifyPropertyChanged的作用是:通知UI更新数据
  Product
public class Product : INotifyDataErrorInfo, INotifyPropertyChanged
    {
        public Product()
        {
        }

        private int idValue;
        public int Id
        {
            get { return idValue; }
            set
            {
                if (IsIdValid(value) && idValue != value)
                {
                    idValue = value;
                    RaisePropertyChanged(new PropertyChangedEventArgs("Id"));
                }
            }
        }

        private string nameValue;
        public string Name
        {
            get { return nameValue; }
            set
            {
                if (IsNameValid(value) && nameValue != value)
                {
                    nameValue = value;
                }
                //RaisePropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }
        //以下代码省略
}

 


posted on 2012-10-24 16:09  傍晚雨  阅读(355)  评论(0编辑  收藏  举报