通过 IDictionary对Silverlight DataGrid 进行动态数据绑定

通 过 IDictionary对Silverlight DataGrid 进行动态数据绑定

      要对DataGrid进行数据绑定,通常我们需要定义具体类,而后建立类的范式集合。如下所示:


  public   class  BasicInfo
    {
        
public   string  id {  set get ; }
        
public   string  name {  set get ; }
        
public   string  tel {  set get ; }
    }

   
private   void  LoadDataByClass()
        {
            var result 
=   new  ObservableCollection < BasicInfo > ();
            result.Add(
new  BasicInfo { id  =   " 1001 " , name  =   " Ivan " , tel  =   " 135462512 "  });
            result.Add(
new  BasicInfo { id  =   " 1002 " , name  =   " Ivan2 " , tel  =   " 135462512 "  });
            result.Add(
new  BasicInfo { id  =   " 1003 " , name  =   " Ivan3 " , tel  =   " 135462512 "  });

            dgshow.ItemsSource 
=  result;
        }

      也就说说,如果要在DataGrid中show集合数据,就需要有具体类的存在【匿名类在绑定的时候会出现运行期异常】。这在很多情况 下是可行的.
      但是有时候,某个集合只需要show一次而已,而我们又不得不给建立具体类以使得其信息能够在DataGrid中显示。 也就是说在要显示的集合的列不确定时,建立具体类实在是有点“浪费”。
      这种情况下,我们可以建立“自己”的集合类【这时候免不了要用 到Indexer】,利用Dictionary来动态建立集合列,从而实现数据的动态添加和显示:


public   class  Row
    {
        Dictionary
< string object >  dictionary  =   new  Dictionary < string object > ();

        
public   object   this [ string  index]
        {
            
get
            {
                
return  dictionary[index];
            }
            
set
            {
                dictionary[index] 
=  value;
            }
        }
    }

       
private   void  LoadDataByDictionary()
        {
            var result 
=   new  ObservableCollection < Row > ();

            Row row 
=   new  Row();
            row[
" id " =   " Dic1001 " ;
            row[
" name " =   " Ivan " ;
            row[
" tel " =   " 123456 " ;
            result.Add(row);

            row 
=   new  Row();
            row[
" id " =   " Dic1002 " ;
            row[
" name " =   " Ivan2 " ;
            row[
" tel " =   " 12345622 " ;
            result.Add(row);

            dgshowDic.ItemsSource 
=  result;

        }

     这样产生的集合,在DataGrid绑定时,绑定的为Row.所以在UI显示之前,要对数据集合进行转换,使得DataGrid中显示需 要显示的列数据:


   public   class  RowIndexConverter : IValueConverter
    {
        
public   object  Convert( object  value, Type targetType,  object  parameter, CultureInfo culture)
        {
            Row row 
=  value  as  Row;
            
string  index  =  parameter  as   string ;
            
return  row[index];
        }

        
public   object  ConvertBack( object  value, Type targetType,  object  parameter, CultureInfo culture)
        {
            
throw   new  NotImplementedException();
        }
    }

        附带Xaml的定义代码:


< UserControl  xmlns:data ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"   x:Class ="CodeProjectDemo.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
     xmlns:local
="clr-namespace:CodeProjectDemo"          
   Loaded
="UserControl_Loaded"   >
    
< UserControl.Resources >
        
< local:RowIndexConverter  x:Key ="RowIndexConverter" ></ local:RowIndexConverter >
        
< local:DateConverter  x:Key ="DateConverter" ></ local:DateConverter >
    
</ UserControl.Resources >
    
< Canvas >
        
< Button  x:Name ="btnDic"  Height ="50"  Width ="150"  Content ="LoadDataBy Dictionary"  FontSize ="12"   Click ="btnDic_Click" ></ Button >
        
< Button  x:Name ="btnCls"  Canvas.Left ="200"   Height ="50"  Width ="150"  Content ="LoadDataBy Class"  FontSize ="12"   Click ="btnCls_Click"   ></ Button >
       
        
< data:DataGrid  x:Name ="dgshow"  Canvas.Top ="70"  Canvas.Left ="400"   Height ="auto"  Width ="150"  AutoGenerateColumns ="True" ></ data:DataGrid >
        
< data:DataGrid  Name ="dgshowDic"  Canvas.Top ="70"  AutoGenerateColumns ="False"  Height ="auto"  IsReadOnly ="False" >
            
< data:DataGrid.Columns >
                
< data:DataGridTextColumn  Header ="id"  Binding =" {Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=id} " />
                
< data:DataGridTextColumn  Header ="name"  Binding =" {Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=name} " />
                
< data:DataGridTextColumn  Header ="tel"  Binding =" {Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=tel} " />
            
</ data:DataGrid.Columns >
        
</ data:DataGrid >
        
< ListBox  x:Name ="MyListBox"  Canvas.Top ="300"  Canvas.Left ="10"   >
            
< ListBox.ItemTemplate >
                
< DataTemplate >
                    
< StackPanel  Orientation ="Horizontal" >
                        
< TextBlock  Text =" {Binding Time1, Converter={StaticResource DateConverter}} "  Margin ="5"  Foreground ="Red" ></ TextBlock >
                        
< TextBlock  Text =" {Binding Time2} "  Margin ="5" ></ TextBlock >
                    
</ StackPanel >
                
</ DataTemplate >
            
</ ListBox.ItemTemplate >
        
</ ListBox >
    
</ Canvas >
</ UserControl >


  ps:注意到 IValueConverter 的应用,可以参考msdn.
     建立转换类后,其将作为一个资源存在, 故而在xaml中要对此资源进行说明【见上述xaml】。
     下面是对此接口的一个简单应用:在ListBox中显示时间前对其进行格式 化。


  public   class  DateConverter : IValueConverter
    {
        
public   object  Convert( object  value, Type targetType,  object  parameter, CultureInfo culture)
        {
            DateTime date 
=  (DateTime)value;
            
return  date.ToShortDateString();
        }

        
public   object  ConvertBack( object  value, Type targetType,  object  parameter, CultureInfo culture)
        {
            
throw   new  NotImplementedException();
        }
    }
    
public   class  MyTime
    {
        
public  DateTime Time1 {  get set ; }
        
public  DateTime Time2 {  get set ; }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值