梁宏达调侃崔永元哪期:Seliverlight 中数据绑定INotifyPropertyChanged 接口的用...

来源:百度文库 编辑:偶看新闻 时间:2024/04/30 03:27:16

Seliverlight 中数据绑定INotifyPropertyChanged 接口的用法 .数据绑定IValueConvert的用法学习笔记

1.数据绑定
    /// 数据绑定是数据源和目标数据之间的一座桥梁 在Silverlighth中这座桥梁有三个方向
    /// oneWay
    /// TwoWay
    /// OneTime
    /// INotifyPropertyChanged
    ///using System.ComponentModel;
    /// 是告诉我们通知的对象
    /// 我们还需要以为信使来完成这个通知
    /// 更改通知就是数据绑定这座桥梁的信使
    /// 它根据数据流方向来通知数据源和目标数据对方的变化
    /// 要实现接口我们需要实现INotifyPropertyChanged接口,INotifyPropertyChanged具有PropertyChanged 事件
    /// 该事件通知绑定引擎源以更改,以便可以更新目标值,
2.数据绑定   在Seliverlight中实现数据转换 需要创建数据转换类 并实先IValueConvert接口的Convert方法和ConvertBack方法
   当数据源传递给目标时调用Convert方法 反之调用ConvertBack方法 Converback方法不可省落。

   [ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
    //在将原数据传递到目标以在Ui显示之前 对原数据进行修改
 //object value,正转递到目标的元数据,
 // Type targetType,目标依赖项属性需要的数据的System.Type
 // object parameter, 要在转换器逻辑中使用的可选参数
 // CultureInfo culture 转换的区域性
 //返回结构 要传递到目标依赖项属性的值
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = (DateTime)value;
        return date.ToShortDateString();
    }

  //再将目标数据传递到原对象之前,对目标数据进行修改
  //System.Window.Data.BindingMode.TwoWay 绑定中进行调用
 //object value,正转递到元的目标数据,
 // Type targetType,源对象需要的数据的System.Type
 // object parameter, 要在转换器逻辑中使用的可选参数
 // CultureInfo culture 转换的区域性
 //返回结构 要传递到源对象的值
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value as string;
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return DependencyProperty.UnsetValue;
    }
}
///数据转换的使用,在Xaml页面中加入转换代码myConverter,在使用前 在UserControl元素中声明命名空间
///xmlns:my="clr-namespace:Sample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ConverterParameterEx"
    Width="400" Height="300">
   
       
          
       

                    ItemsSource="{Binding}">
           
               
                   
                       
                       
                       
                  

               

           

       

   

using System;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Data;

namespace ConverterParameterEx
{
    public partial class Page : UserControl
    {

        public ObservableCollection MyMusic =
            new ObservableCollection();
        public Page()
        {
            InitializeComponent();

            // Add items to the collection.
            MyMusic.Add(new Recording("Chris Sells", "Chris Sells Live",
                new DateTime(2008, 2, 5)));
            MyMusic.Add(new Recording("Luka Abrus",
                "The Road to Redmond", new DateTime(2007, 4, 3)));
            MyMusic.Add(new Recording("Jim Hance",
                "The Best of Jim Hance", new DateTime(2007, 2, 6)));

            // Set the data context for the combo box.
            MusicCombo.DataContext = MyMusic;
        }
    }

    // Simple business object.
    public class Recording
    {
        public Recording() { }
        public Recording(string artistName, string cdName, DateTime release)
        {
            Artist = artistName;
            Name = cdName;
            ReleaseDate = release;
        }
        public string Artist { get; set; }
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
    }

    public class DateFormatter : IValueConverter
    {
        // This converts the DateTime object to the string to display.
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            // Retrieve the format string and use it to format the value.
            string formatString = parameter as string;
            if (!string.IsNullOrEmpty(formatString))
            {
                return string.Format(culture, formatString, value);

            }
            // If the format string is null or empty, simply call ToString()
            // on the value.
            return value.ToString();
        }

        // No need to implement converting back on a one-way binding
        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}