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

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

概述  [源码下载] 稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue, CollectionViewSource 作者: webabcd 介绍 Silverlight 4.0 绑定相关的增强: DependencyObject Binding - 新增了   [源码下载]

稳扎稳打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

代码 < 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
[源码下载] 总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue,全部内容,希望文章能够帮你解决稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue,所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存