要对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, Type targetType, object parameter, 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中显示时间前对其进行格式化。
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; }
}
来源:https://www.cnblogs.com/Ivan-Yan/archive/2009/05/27/1490667.html
标签