
要对DataGrID进行数据绑定,通常我们需要定义具体类,而后建立类的范式集合。如下所示:
Codepublic 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中显示需 要显示的列数据:
Codepublic 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, CultureInfo culture)
{
throw new NotImplementedException();
}
}
附带Xaml的定义代码:
Code< 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中显示时间前对其进行格式 化。
Code
public class DateConverter : IValueConverter
{
public object Convert( object value, CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
public object ConvertBack( object value, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class MyTime
{
public DateTime Time1 { get ; set ; }
public DateTime Time2 { get ; set ; } } 总结
以上是内存溢出为你收集整理的通过 IDictionary对Silverlight DataGrid 进行动态数据绑定全部内容,希望文章能够帮你解决通过 IDictionary对Silverlight DataGrid 进行动态数据绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)