微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue,

  [源码下载]

稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定,索引器绑定,StringFormat,TargetNullValue和FallbackValue,CollectionViewSource

作者: webabcd
介绍
Silverlight 4.0 绑定相关的增强:
  • DependencyObject Binding - 新增了对 DependencyObject 绑定的支持 
  • Indexer Binding - 新增了对索引器绑定的支持 
  • StringFormat - 指定绑定数据的显示格式 
  • TargetNullValue - 当绑定数据为 null 时所需要显示的值 
  • FallbackValue - 当绑定失败(无法返回值)的时候所需要显示的值 
  • CollectionViewSource - 实现了 ICollectionView 的类,可以通过它对数据排序、筛选和分组 

在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、演示如何绑定到 DependencyObject
DependencyObjectBinding.xaml

代码
< navigation:Page  x:Class ="Silverlight40.Binding.DependencyObjectBinding"  
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           Title
="DependencyObjectBinding Page" >
    
< Grid  x:Name ="LayoutRoot" >
        
< StackPanel  HorizontalAlignment ="Left" >

            
<!--  
                Silverlight 3.0 支持绑定到 FrameworkElement 
                    TextBox 继承自 FrameworkElement
            
-->
            
< TextBox  Text =" {Binding ElementName=slider, Path=Value} "   />

            
<!--  
                Silverlight 4.0 中新增了对 DependencyObject 绑定的支持
                    RotateTransform 继承自 DependencyObject
            
-->
            
< Rectangle  Width ="100"  Height ="100"  RenderTransformOrigin ="0.5, 0.5"  Fill ="Red" >
                
< Rectangle.RenderTransform >
                    
< RotateTransform  Angle =" {Binding ElementName=slider, Path=Value} "   />
                
</ Rectangle.RenderTransform >
            
</ Rectangle  >

            
< Slider  Name ="slider"  Height ="20"  Minimum ="0"  Maximum ="360"   />

        
</ StackPanel >
    
</ Grid >
</ navigation:Page >

2、演示如何绑定到索引器
IndexerBinding.xaml

代码
< navigation:Page  x:Class ="Silverlight40.Binding.IndexerBinding"  
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           Title
="IndexerBinding Page" >
    
< Grid  x:Name ="LayoutRoot" >

        
<!--
            用于演示索引器的绑定
        
-->
        
< TextBlock  Name ="textBlock"  Text =" {Binding Path=[3] } "   />

    
</ Grid >
</ navigation:Page >

IndexerBinding.xaml.cs

代码
/*
 * Silverlight 4.0 中新增了对索引器绑定的支持,索引的类型必须实现 IList
 
*/

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.Windows.Navigation;

namespace  Silverlight40.Binding
{
    
public   partial   class  IndexerBinding : Page
    {
        
public  IndexerBinding()
        {
            InitializeComponent();
        }

        
protected   override   void  OnNavigatedTo(NavigationEventArgs e)
        {
            List
< string >  list  =   new  List < string > ();
            
for  ( int  i  =   0 ; i  <   10 ; i ++ )
            {
                list.Add(
" 索引: "   +  i.ToString());
            }

            textBlock.DataContext 
=  list;
        }
    }
}

3、演示在绑定时使用 StringFormat 来指定数据的显示格式
StringFormat.xaml

代码
< navigation:Page  x:Class ="Silverlight40.Binding.StringFormat"  
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           Title
="StringFormat Page" >
    
< Grid  x:Name ="LayoutRoot" >

        
<!--
            StringFormat - 指定绑定数据的显示格式
        
-->
        
< TextBlock  Name ="textBlock"  Text =" {Binding StringFormat='yyyy-MM-dd HH:mm:ss'} "   />
        
    
</ Grid >
</ navigation:Page >

StringFormat.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.Windows.Navigation;

namespace  Silverlight40.Binding
{
    
public   partial   class  StringFormat : Page
    {
        
public  StringFormat()
        {
            InitializeComponent();
        }

        
protected   override   void  OnNavigatedTo(NavigationEventArgs e)
        {
            textBlock.DataContext 
=  DateTime.Now;
        }
    }
}

4、演示 TargetNullValue 和 FallbackValue 的效果
TargetNullValueFallbackValue.xaml

代码
< navigation:Page  x:Class ="Silverlight40.Binding.TargetNullValueFallbackValue"  
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           Title
="TargetNullValueFallbackValue Page" >
    
< Grid  x:Name ="LayoutRoot" >
        
< StackPanel  HorizontalAlignment ="Left"  Name ="stackPanel" >

            
<!--
                FallbackValue - 当绑定失败(无法返回值)的时候所需要显示的值
            
-->
            
< TextBlock  Text =" {Binding Path=xxx, FallbackValue='绑定失败时的认值'} "   />

            
<!--
                TargetNullValue - 当绑定数据为 null 时所需要显示的值
            
-->
            
< TextBlock  Text =" {Binding TargetNullValue='绑定返回值为 null'} "   />

        
</ StackPanel >
    
</ Grid >
</ navigation:Page >

TargetNullValueFallbackValue.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.Windows.Navigation;

namespace  Silverlight40.Binding
{
    
public   partial   class  TargetNullValueFallbackValue : Page
    {
        
public  TargetNullValueFallbackValue()
        {
            InitializeComponent();
        }

        
protected   override   void  OnNavigatedTo(NavigationEventArgs e)
        {
            stackPanel.DataContext 
=   null ;
        }
    }
}

5、演示 CollectionViewSource 的应用
Product.cs

代码
using  System;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Ink;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;

namespace  Silverlight40.Binding
{
    
//  实体类
     public   class  Product
    {
        
public   int  ProductId {  get set ; }
        
public   string  Name {  get set ; }
        
public   string  Category {  get set ; }
    }
}

ProductCollection.cs

代码
using  System;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Ink;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;

using  System.Collections.Generic;

namespace  Silverlight40.Binding
{
    
public   class  ProductCollection : List < Product >
    {
        
    }
}

CollectionViewSource.xaml

代码 @H_404_1431@
< navigation:Page  x:Class ="Silverlight40.Binding.CollectionViewSource"  
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:sdk
="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
           xmlns:c
="clr-namespace:System.ComponentModel;assembly=System.Windows"
           xmlns:local
="clr-namespace:Silverlight40.Binding"
           Title
="CollectionViewSource Page" >
    
< Grid  x:Name ="LayoutRoot" >

        
< Grid.Resources >
            
< local:ProductCollection  x:Key ="products" >
                
< local:Product  ProductId ="1"  Name ="abc"  Category ="CategoryA"   />
                
< local:Product  ProductId ="2"  Name ="xyz"  Category ="CategoryA"   />
                
< local:Product  ProductId ="3"  Name ="webabcd"  Category ="CategoryB"   />
            
</ local:ProductCollection >

            
<!--
                CollectionViewSource - 实现了 ICollectionView 的类,可以通过它对数据排序、筛选和分组
                    CollectionViewSource.sortDescriptions - 指定排序方式
                        PropertyName - 指定排序的字段
                        Direction - 指定按升序还是降序排序
                    CollectionViewSource.GroupDescriptions - 指定分组方式
                        PropertyName - 指定分组的字段
                        StringComparison - 指定是否区分大小写等
            
-->
            
< CollectionViewSource  x:Name ="dataSource"  Source =" {StaticResource products} " >
                
< CollectionViewSource.sortDescriptions >
                    
< c:SortDescription  PropertyName ="Name"  Direction ="Descending"   />
                
</ CollectionViewSource.sortDescriptions >
                
< CollectionViewSource.GroupDescriptions >
                    
< PropertyGroupDescription  PropertyName ="Category"  StringComparison ="CurrentCultureIgnoreCase"   />
                
</ CollectionViewSource.GroupDescriptions >
            
</ CollectionViewSource >
        
</ Grid.Resources >

        
< sdk:DataGrid  ItemsSource =" {Binding Source={StaticResource dataSource}} "   />

    
</ Grid >
</ navigation:Page >

OK
[源码下载]
@H_404_1782@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐