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

[索引页]
[源码下载]


稳扎稳打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 { getset; }
        
public string Name { getset; }
        
public string Category { getset; }
    }
}


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

posted @ 2010-09-06 08:43  webabcd  阅读(5156)  评论(2编辑  收藏  举报