稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 绑定数据到ListBox

简介:
[索引页]
[源码下载]


稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 详解ListBox


作者: webabcd


介绍
Silverlight 2.0 详解DataGrid, 绑定数据到ListBox:
    AutoGenerateColumns - 是否根据数据源自动生成列
    RowDetailsVisibilityMode - 显示相应的行的详细数据时所使用的显示模式
    DataGrid.Columns - 手工定义DataGrid的列
    DataGrid.RowDetailsTemplate - 用于显示相应的行的详细数据的模板
    AreRowDetailsFrozen - 是否冻结 RowDetailsTemplate
    GridLinesVisibility - 表格分隔线的显示方式
    RowBackground - 奇数数据行背景
    AlternatingRowBackground - 偶数数据行背景
    IsReadOnly - 单元格是否只读
    FrozenColumnCount - 表格所冻结的列的总数(从左边开始数)
    SelectionMode - 行的选中模式
    CanUserReorderColumns - 是否允许拖动列
    CanUserResizeColumns - 是否允许改变列的宽度
    CanUserSortColumns - 是否允许列的排序
    VerticalGridLinesBrush - 改变表格的垂直分隔线的 Brush
    HorizontalGridLinesBrush - 改变表格的水平分隔线的 Brush
    HeadersVisibility - 表头(包括列头和行头)的显示方式


在线DEMO
http://webabcd.blog.51cto.com/1787395/342779


示例
SourceDataModel.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 Silverlight20.Data 

         public  class SourceDataModel 
        { 
                 public  string Name { get; set; } 
                 public  int Age { get; set; } 
                 public DateTime DayOfBirth { get; set; } 
                 public  bool Male { get; set; } 
        } 
}
 
SourceData.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.ObjectModel; 
 
namespace Silverlight20.Data 

         public  class SourceData 
        { 
                 //    ObservableCollection<T> 内置实现了 INotifyCollectionChanged 接口(可直接应用于 OneWay 和 TwoWay 的绑定模式) 
                 public ObservableCollection<SourceDataModel> GetData() 
                { 
                        var source =  new ObservableCollection<SourceDataModel>(); 
 
                         for ( int i = 0; i < 100; i++) 
                        { 
                                source.Add( 
                                         new SourceDataModel 
                                        { 
                                                Name =  "Name" + i.ToString().PadLeft(4, '0'), 
                                                Age =  new Random(i).Next(20, 60), 
                                                DayOfBirth = DateTime.Now, 
                                                Male = Convert.ToBoolean(i % 2) 
                                        }); 
                        } 
 
                         return source; 
                } 
        } 
}
 

1、DataGrid01.xaml
<UserControl x:Class="Silverlight20.Data.DataGrid01" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"    
        xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"> 
        <StackPanel HorizontalAlignment="Left"> 

                <CheckBox Content="是否冻结 RowDetailsTemplate" Margin="5" 
                        Checked="chkFreezeRowDetails_Changed" Unchecked="chkFreezeRowDetails_Changed" /> 

                <!-- 
                AutoGenerateColumns - 是否根据数据源自动生成列。默认值为 true 
                RowDetailsVisibilityMode - 显示相应的行的详细数据时所使用的显示模式 [System.Windows.Controls.DataGridRowDetailsVisibilityMode枚举] 
                        DataGridRowDetailsVisibilityMode.Collapsed - 总不显示         
                        DataGridRowDetailsVisibilityMode.Visible - 总是显示     
                        DataGridRowDetailsVisibilityMode.VisibleWhenSelected - 只显示选中行的详细数据。默认值                    
                --> 
                <data:DataGrid x:Name="DataGrid1" Margin="5" 
                        Width="400" Height="200"    
                        AutoGenerateColumns="False"    
                        > 

                        <!-- 
                        DataGrid.Columns - 手工定义DataGrid的列 
                                DataGridTextColumn - 该列的单元格内文本方式显示 
                                DataGridCheckBoxColumn - 该列的单元格以复选框方式显示 
                                DataGridTemplateColumn - 该列的单元格以自定义方式显示 
                                        DataGridTemplateColumn.CellTemplate - 显示模式下的单元格模板 
                                        DataGridTemplateColumn.CellEditingTemplate - 编辑模式下的单元格模板 
                        --> 
                        <data:DataGrid.Columns> 
                                <data:DataGridTextColumn Header="姓名" Binding="{Binding Name}" /> 
                                <data:DataGridTextColumn Header="生日" Binding="{Binding DayOfBirth}" /> 
                                <data:DataGridTextColumn Header="年龄" Binding="{Binding Age}" /> 
                                <data:DataGridCheckBoxColumn Header="性别" Binding="{Binding Male}" /> 
                                <data:DataGridTemplateColumn Header="生日"> 
                                        <data:DataGridTemplateColumn.CellTemplate> 
                                                <DataTemplate> 
                                                        <Grid> 
                                                                <Rectangle Fill="Red" Margin="2" /> 
                                                                <TextBlock Text="{Binding DayOfBirth}" Foreground="Yellow" /> 
                                                        </Grid> 
                                                </DataTemplate> 
                                        </data:DataGridTemplateColumn.CellTemplate> 
                                        <data:DataGridTemplateColumn.CellEditingTemplate> 
                                                <DataTemplate> 
                                                        <basics:DatePicker SelectedDate="{Binding DayOfBirth}" /> 
                                                </DataTemplate> 
                                        </data:DataGridTemplateColumn.CellEditingTemplate> 
                                </data:DataGridTemplateColumn> 
                        </data:DataGrid.Columns> 

                        <!-- 
                        DataGrid.RowDetailsTemplate - 用于显示相应的行的详细数据的模板 
                        --> 
                        <data:DataGrid.RowDetailsTemplate> 
                                <DataTemplate> 
                                        <StackPanel Margin="5" Background="Aqua" Height="100"> 
                                                <TextBlock Text="{Binding Name}" Margin="5"    /> 
                                                <TextBlock Text="{Binding Age}" Margin="5"    /> 
                                                <TextBlock Text="{Binding DayOfBirth}" Margin="5"    /> 
                                        </StackPanel> 
                                </DataTemplate> 
                        </data:DataGrid.RowDetailsTemplate> 

                </data:DataGrid> 

        </StackPanel> 
</UserControl>
 
DataGrid01.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; 
 
namespace Silverlight20.Data 

         public partial  class DataGrid01 : UserControl 
        { 
                 public DataGrid01() 
                { 
                        InitializeComponent(); 
 
                        BindData(); 
                } 
 
                 void BindData() 
                { 
                        var source =  new Data.SourceData(); 
 
                         // 设置 DataGrid 的数据源 
                        DataGrid1.ItemsSource = source.GetData(); 
                } 
 
                 private  void chkFreezeRowDetails_Changed( object sender, RoutedEventArgs e) 
                { 
                         // AreRowDetailsFrozen - 是否冻结 RowDetailsTemplate 。 默认值为 false 
                         //         如果等于 true ,那么在 DataGrid 的水平滚动条滚动的时候 RowDetailsTemplate 不会跟着滚动 
 
                        CheckBox chk = sender  as CheckBox; 
 
                         if (DataGrid1 !=  null
                                DataGrid1.AreRowDetailsFrozen = ( bool)chk.IsChecked; 
                } 
        } 
}
 
 



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/343109 ,如需转载请自行联系原作者


相关文章

热门文章

最新文章