稳扎稳打Silverlight(15) - 2.0数据之一次绑定, 单向绑定, 双向绑定, INotifyPropertyChanged, 数据转换

稳扎稳打Silverlight(15) - 2.0数据之一次绑定, 单向绑定, 双向绑定, INotifyPropertyChanged, 数据转换,第1张

概述[索引页] [源码下载] 稳扎稳打Silverlight(15) - 2.0数据之一次绑定, 单向绑定, 双向绑定, INotifyPropertyChanged, 数据转换, 数据验证 作者: webabcd 介绍 Silverlight 2.0 数据绑定:     Binding - 将绑定目标对象的属性与数据源联接起来         Source - 绑定的数据源         Mode [索引页]
[源码下载]


稳扎稳打Silverlight(15) - 2.0数据之一次绑定,单向绑定,双向绑定,INotifyPropertyChanged,数据转换,数据验证

作者: webabcd


介绍
Silverlight 2.0 数据绑定:
    Binding - 将绑定目标对象的属性与数据源联接起来
        Source - 绑定的数据源
        Mode - 绑定的数据流的方向 [System.windows.Data.BindingMode枚举]
            BindingMode.OneTime - 一次绑定。创建绑定时一次性地更新绑定目标对象的属性
            BindingMode.OneWay - 单向绑定(默认值)。数据源的改变会自动通知到绑定目标对象的属性
            BindingMode.TwoWay - 双向绑定。数据源或绑定目标对象的属性的值发生改变时会互相通知。显然,做数据验证的话一定要是双向绑定
        Path - 需要绑定的属性名称
        NotifyOnValIDationError - 产生验证错误时是否触发 BindingValIDationError 事件。默认值为 false
        ValIDatesOnExceptions - 产生验证错误时绑定引擎是否要报告错误。默认值为 false
    INotifyPropertyChanged - 向客户端发出某一属性值已更改的通知
    IValueConverter - 值转换接口,将一个类型的值转换为另一个类型的值。它提供了一种将自定义逻辑应用于绑定的方式
        Convert - 正向转换器。将值从数据源传给绑定目标时,数据绑定引擎会调用此方法
        ConvertBack - 反向转换器。将值从绑定目标传给数据源时,数据绑定引擎会调用此方法
    BindingValIDationError - 出现验证错误或解决上次验证错误则触发此事件


在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.html  


示例
1、NotifyProperty.xaml(演示INotifyPropertyChanged) <UserControl x:Class="Silverlight20.Data.NotifyProperty"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel HorizontalAlignment="left">

                <!--
                Binding - 将绑定目标对象的属性与数据源联接起来(本例为将 Ellipse的Fill属性 与 Mycolor的Brush属性 相联)
                Mode - Binding 的扩展属性之一,默认为 OneWay(单向绑定),即数据源的改变会自动通知到绑定目标对象的属性
                -->
                <Ellipse x:name="ellipse" WIDth="100" Height="50" Fill="{Binding Brush,Mode=OneWay}" MouseleftbuttonDown="ellipse_MouseleftbuttonDown" />

        </StackPanel>
</UserControl>   NotifyProperty.xaml.cs

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;


using System.ComponentModel;


namespace Silverlight20.Data

{

         public partial class NotifyProperty : UserControl

        {

                Mycolor _mycolor;


                 public Notifyproperty()

                {

                        InitializeComponent();


                         this.Loaded += new RoutedEventHandler(NotifyProperty_Loaded);

                }


                 voID NotifyProperty_Loaded( object sender,RoutedEventArgs e)

                {

                        _mycolor = new Mycolor { Brush = new SolIDcolorBrush(colors.Red) };


                         // DataContext - FrameworkElement 做数据绑定时的数据上下文

                         // 将 Mycolor 对象绑定到 Ellipse

                        ellipse.DataContext = _mycolor;

                }


                 private voID ellipse_MouseleftbuttonDown( object sender,MousebuttonEventArgs e)

                {

                         // 鼠标按下后修改 Mycolor 对象的属性

                         // 如果Mycolor实现了INotifyPropertyChanged接口,并且绑定目标的BindingMode为OneWay或者TwoWay的话则会自动通知到绑定目标

                         if (_mycolor.Brush.color == colors.Red)

                                _mycolor.Brush = new SolIDcolorBrush(colors.Green);

                         else

                                _mycolor.Brush = new SolIDcolorBrush(colors.Red);

                }

        }


         /*

         * INotifyPropertyChanged - 向客户端发出某一属性值已更改的通知

         * 使用 OneWay 或者 TwoWay 的话必须要实现 INotifyPropertyChanged 接口

        */

         public class Mycolor : INotifyPropertyChanged

        {

                 private SolIDcolorBrush _brush;

                 public SolIDcolorBrush Brush

                {

                        get { return _brush; }

                        set

                        {

                                _brush = value;

                                 if (PropertyChanged != null)

                                {

                                         // 触发 PropertyChanged 事件,事件参数为属性名称

                                        PropertyChanged( this,new PropertyChangedEventArgs( "Brush"));

                                }

                        }

                }


                 // 声明一个 PropertyChanged 事件

                 public event PropertyChangedEventHandler PropertyChanged;

        }

}   2、Conversion.xaml(演示数据转换)
<!--引用转换器所在的命名空间-->
<UserControl x:Class="Silverlight20.Data.Conversion"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:custom="clr-namespace:Silverlight20.Data">
        
        <UserControl.Resources>
        
                <!--在资源中声明转换器-->
                <custom:colorEnumToBrush x:Key="converter" />
                
        </UserControl.Resources>
        
        <StackPanel HorizontalAlignment="left">

                <!--
                Converter - 用于指定转换器
                ConverterParameter - 转换器所使用的参数
                ConverterCulture - 转换器所使用的区域信息
                -->
                <Ellipse x:name="ellipse" WIDth="100" Height="50" MouseleftbuttonDown="ellipse_MouseleftbuttonDown"    
                        Fill="{Binding colorEnum,Converter={StaticResource converter},ConverterParameter=10}"    
                />

        </StackPanel>
</UserControl>   Conversion.xaml.cs

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;


using System.ComponentModel;

using System.windows.Data;


namespace Silverlight20.Data

{

         public partial class Conversion : UserControl

        {

                MycolorEnum _mycolorEnum;


                 public Conversion()

                {

                        InitializeComponent();


                         this.Loaded += new RoutedEventHandler(Conversion_Loaded);

                }


                 voID Conversion_Loaded( object sender,RoutedEventArgs e)

                {

                        _mycolorEnum = new MycolorEnum() { colorEnum = colorEnum.Red };


                         // DataContext - FrameworkElement 做数据绑定时的数据上下文

                         // 将 MycolorEnum 对象绑定到 Ellipse

                        ellipse.DataContext = _mycolorEnum;

                }


                 private voID ellipse_MouseleftbuttonDown( object sender,MousebuttonEventArgs e)

                {

                         // 鼠标按下后修改 MycolorEnum 对象的属性

                         if (_mycolorEnum.colorEnum == colorEnum.Red)

                                _mycolorEnum.colorEnum = colorEnum.Green;

                         else

                                _mycolorEnum.colorEnum = colorEnum.Red;

                }

        }


         /*

         * IValueConverter - 值转换接口,将一个类型的值转换为另一个类型的值。它提供了一种将自定义逻辑应用于绑定的方式

         *         Convert - 正向转换器。将值从数据源传给绑定目标时,数据绑定引擎会调用此方法

         *         ConvertBack - 反向转换器。将值从绑定目标传给数据源时,数据绑定引擎会调用此方法

        */

         public class colorEnumToBrush : IValueConverter

        {

                 /// <summary>

                 /// 正向转换器。将值从数据源传给绑定目标时,数据绑定引擎会调用此方法

                 /// </summary>

                 /// <param name="value">转换之前的值</param>

                 /// <param name="targettype">转换之后的类型</param>

                 /// <param name="parameter">转换器所使用的参数</param>

                 /// <param name="culture">转换器所使用的区域信息</param>

                 /// <returns>转换后的值</returns>

                 public object Convert( object value,Type targettype,object parameter,System.Globalization.CultureInfo culture)

                {

                         // 将 colorEnum 类型的值转换为 SolIDcolorBrush 类型的值


                         // parameter == 10


                        colorEnum color = (colorEnum)value;


                        SolIDcolorBrush brush = new SolIDcolorBrush(colors.Red);


                         if (color == colorEnum.Green)

                                brush = new SolIDcolorBrush(colors.Green);


                         return brush;

                }


                 /// <summary>

                 /// 反向转换器。将值从绑定目标传给数据源时,数据绑定引擎会调用此方法

                 /// </summary>

                 /// <param name="value">转换之前的值</param>

                 /// <param name="targettype">转换之后的类型</param>

                 /// <param name="parameter">转换器所使用的参数</param>

                 /// <param name="culture">转换器所使用的区域信息</param>

                 /// <returns>转换后的值</returns>

                 public object ConvertBack( object value,System.Globalization.CultureInfo culture)

                {

                         return null;

                }

        }


         /*

         * INotifyPropertyChanged - 向客户端发出某一属性值已更改的通知

         * 使用 OneWay 或者 TwoWay 的话必须要实现 INotifyPropertyChanged 接口

        */

         public class MycolorEnum : INotifyPropertyChanged

        {

                 private colorEnum _colorEnum;

                 public colorEnum colorEnum

                {

                        get { return _colorEnum; }

                        set

                        {

                                _colorEnum = value;

                                 if (PropertyChanged != null)

                                {

                                         // 触发 PropertyChanged 事件,事件参数为属性名称

                                        PropertyChanged( this,new PropertyChangedEventArgs( "colorEnum"));

                                }

                        }

                }


                 // 声明一个 PropertyChanged 事件

                 public event PropertyChangedEventHandler PropertyChanged;

        }


         /// <summary>

         /// 自定义的 colorEnum 枚举

         /// </summary>

         public enum colorEnum

        {

                Red,

                Green

        }

}     3、ValIDation.xaml(演示数据验证)
<!--引用验证类所在的命名空间-->
<UserControl x:Class="Silverlight20.Data.ValIDation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:custom="clr-namespace:Silverlight20.Data">
        
        <!--
        BindingValIDationError - 出现验证错误或解决上次验证错误则触发此事件
        -->
        <StackPanel HorizontalAlignment="left" BindingValIDationError="StackPanel_BindingValIDationError" >
                
                <StackPanel.Resources>
                
                        <!--在资源中声明验证类-->
                        <custom:MyValIDation x:name="myValIDation"/>
                        
                </StackPanel.Resources>
                
                <TextBox x:name="textBox" WIDth="200" margin="5">
                        <TextBox.Text>
                        
                                <!--
                                Binding - 将绑定目标对象的属性与数据源联接起来
                                        Source - 绑定的数据源
                                        Mode - 绑定的数据流的方向 [System.windows.Data.BindingMode枚举]
                                                BindingMode.OneTime - 一次绑定。创建绑定时一次性地更新绑定目标对象的属性
                                                BindingMode.OneWay - 单向绑定(默认值)。数据源的改变会自动通知到绑定目标对象的属性
                                                BindingMode.TwoWay - 双向绑定。数据源或绑定目标对象的属性的值发生改变时会互相通知。显然,做数据验证的话一定要是双向绑定
                                        Path - 需要绑定的属性名称
                                        NotifyOnValIDationError - 产生验证错误时是否触发 BindingValIDationError 事件。默认值为 false
                                        ValIDatesOnExceptions - 产生验证错误时绑定引擎是否要报告错误。默认值为 false
                                -->
                                <Binding    
                                        Source="{StaticResource myValIDation}"    
                                        Mode="TwoWay"    
                                        Path="Count"
                                        NotifyOnValIDationError="True"    
                                        ValIDatesOnExceptions="True"
                                />
                                        
                        </TextBox.Text>
                </TextBox>
                
                <TextBox x:name="textBox2" WIDth="200"    margin="5" />
                        
        </StackPanel>
        
</UserControl>   ValIDation.xaml.cs

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;


using System.Text.RegularExpressions;


namespace Silverlight20.Data

{

         public partial class ValIDation : UserControl

        {

                 public ValIDation()

                {

                        InitializeComponent();

                }


                 private voID StackPanel_BindingValIDationError( object sender,ValIDationErrorEventArgs e)

                {

                         // ValIDationErrorEventArgs - 用于提供 BindingValIDationError 事件的一些信息

                         //         ValIDationErrorEventArgs.Action - 验证状态

                         //         ValIDationErrorEventArgs.Error - 触发 BindingValIDationError 事件的错误信息

                         //         ValIDationErrorEventArgs.Handled - 标记该路由事件是否已被处理

                         // ValIDationErrorEventAction.Added - 因为出现验证错误而触发此事件

                         // ValIDationErrorEventAction.Removed - 因为解决上次验证错误而触发此事件


                         if (e.Action == ValIDationErrorEventAction.Added)

                        {

                                textBox.Background = new SolIDcolorBrush(colors.Red);

                                textBox.Text = e.Error.Exception.Message;

                        }

                         else if (e.Action == ValIDationErrorEventAction.Removed)

                        {

                                textBox.Background = new SolIDcolorBrush(colors.White);

                        }

                }

        }


         /// <summary>

         /// 验证类。验证是否为正整数

         /// </summary>

         public class MyValIDation

        {

                 private string _count;

                 public string Count

                {

                        get { return _count; }

                        set

                        {

                                 if (!Regex.IsMatch(value,@"^\d+$"))

                                {

                                         // 绑定引擎可以报告由属性的 setter 抛出的异常,也可以报告由转换器(IValueConverter)抛出的异常

                                         throw new Exception( "必须是正整数");

                                }


                                _count = value;

                        }

                }

        }

}     OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(15) - 2.0数据之一次绑定, 单向绑定, 双向绑定, INotifyPropertyChanged, 数据转换全部内容,希望文章能够帮你解决稳扎稳打Silverlight(15) - 2.0数据之一次绑定, 单向绑定, 双向绑定, INotifyPropertyChanged, 数据转换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053060.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存