Sliverlight学习笔记七DataGrid控件

这一节是DataGrid,绑定数据源

效果如下图:

 

1.DataGridDemo.xaml

<UserControl xmlns:datagrid="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="Silverlight.Common.View.DataGridDemo"
    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:dataform="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
              xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <StackPanel>
        <ContentControl Content="DataGrid" />
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition  Height="310"/>
                <RowDefinition  Height="25"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="180"/>
                <ColumnDefinition  Width="840"/>
                <ColumnDefinition  Width="*"/>
            </Grid.ColumnDefinitions>
            <StackPanel Grid.Column="0">
                <CheckBox Content="不可编辑" x:Name="IsEditable" IsChecked="{Binding IsReadOnly, ElementName=dataGrid, Mode=TwoWay}"  Margin="4" />
                <CheckBox Content="否可更改列的显示顺序" IsChecked="{Binding CanUserReorderColumns, ElementName=dataGrid, Mode=TwoWay}" Margin="4" />
                <CheckBox Content="列宽是否可变" IsChecked="{Binding CanUserResizeColumns, ElementName=dataGrid, Mode=TwoWay}" Margin="4" />
                <CheckBox Content="是否可单击列标题来对列排序" IsChecked="{Binding CanUserSortColumns, ElementName=dataGrid, Mode=TwoWay}" Margin="4" />
                <TextBlock  Text="无法水平滚动的列数" Margin="4" />
                <input:NumericUpDown Width="100" HorizontalAlignment="Left" Minimum="0" Margin="4" Value="{Binding FrozenColumnCount, ElementName=dataGrid, Mode=TwoWay}" />
                <CheckBox x:Name="cbGroupping" Content="是否分组显示"  Margin="4" Click="OnClick" />
                <CheckBox Content="数据格式验证" IsChecked="{Binding IsValid, ElementName=dataGrid, Mode=TwoWay}" Margin="4" />
                <TextBlock  Text="何时显示行的详细信息部分:" Margin="4" />
                <ComboBox HorizontalAlignment="Left" Width="auto" SelectedItem="{Binding RowDetailsVisibilityMode, ElementName=dataGrid, Mode=TwoWay}" Margin="4" SelectedIndex="2">
                    <sys:String>Collapsed</sys:String>
                    <sys:String>Visible</sys:String>
                    <sys:String>VisibleWhenSelected</sys:String>
                </ComboBox>
                <TextBlock Text="选择行为:" Margin="4" />
                <ComboBox HorizontalAlignment="Left" Width="70" SelectedItem="{Binding SelectionMode, ElementName=dataGrid, Mode=TwoWay}" Margin="4" SelectedIndex="1">
                    <sys:String>Single</sys:String>
                    <sys:String>Extended</sys:String>
                </ComboBox>
            </StackPanel>
            <datagrid:DataGrid x:Name="dataGrid"  Width="820" Height="310" ColumnWidth="100"  ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="4" MaxHeight="300" Grid.Column="1">
                <datagrid:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <StackPanel Background="LightBlue">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="This item has details." />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Here is some data: " />
                                <TextBlock Text="{Binding Name}" />
                                <TextBlock Text=" " />
                                <TextBlock Text="{Binding Password}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </datagrid:DataGrid.RowDetailsTemplate>
            </datagrid:DataGrid>
            <datagrid:DataPager Width="820" Height="24" Grid.Row="1" Margin="0,-15,0,0" Grid.Column="1" Source="{Binding}" PageSize="10" />
            <StackPanel Grid.Column="2">
     
                    <dataform:DataForm x:Name="dataForm"  Margin="4" Grid.Column="2" ItemsSource="{Binding}" >
                    </dataform:DataForm>

            </StackPanel>
        </Grid>
    </StackPanel>
</UserControl>

 

2.DataGridDemo.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 Silverlight.Common.Core;

using System.Windows.Data;

namespace Silverlight.Common.View
{
    public partial class DataGridDemo : UserControl
    {
        public DataGridDemo()
        {
            InitializeComponent();

    //绑定数据源,赋值
            PagedCollectionView pcv = new PagedCollectionView(UserList.GetUserList());
            DataContext = pcv;
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            if ((bool)this.cbGroupping.IsChecked)
            {

      //分组显示
                PagedCollectionView Groupping = new PagedCollectionView(UserList.GetUserList());
                Groupping.GroupDescriptions.Add(new PropertyGroupDescription("Password"));
                Groupping.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
                DataContext = Groupping;
            }

            else
            {
                DataContext = UserList.GetUserList();
            }
        }
    }
}

 

3.User.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 Silverlight.Common.Core
{
    public class User
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public virtual int UserID { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public virtual string UserName { get; set; }

        /// <summary>
        /// 用户姓名
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// 用户密码
        /// </summary>
        public virtual string Password { get; set; }

        /// <summary>
        /// 用户描述
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public virtual bool IsEnabled { get; set; }

        /// <summary>
        /// 密码恢复问题
        /// </summary>
        public virtual string Question { get; set; }

        /// <summary>
        /// 密码恢复答案
        /// </summary>
        public virtual string Answer { get; set; }
    }
}

 

4.UserList.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 Silverlight.Common.Core
{
    public static class UserList
    {
        public static IList<User> GetUserList()
        {
            IList<User> userList = new List<User>();
            userList.Add(new User { Answer="我的家乡在哪?",Description="超级管理员权限",IsEnabled=true,Name="吴建强",Password="11111",Question="山西省神池县",UserID=1,UserName="admin"});
            userList.Add(new User { Answer = "我最喜欢的国家?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "sfsgfsd", Question = "韩国", UserID = 2, UserName = "lxt" });
            userList.Add(new User { Answer = "我最喜欢的人?", Description = "普通用户权限", IsEnabled = true, Name = "李秀婷", Password = "11111", Question = "山西省神池县", UserID = 3, UserName = "wjq" });
            userList.Add(new User { Answer = "我最喜欢的明星?", Description = "普通用户权限", IsEnabled = true, Name = "权相宇,李连杰", Password = "11111", Question = "权相宇,李连杰", UserID = 4, UserName = "qxy" });
            userList.Add(new User { Answer = "我最爱看的电影?", Description = "普通用户权限", IsEnabled = true, Name = "李连杰", Password = "11111", Question = "精武英雄", UserID = 5, UserName = "lxj" });
            userList.Add(new User { Answer = "我最爱看的电视剧?", Description = "普通用户权限", IsEnabled = true, Name = "权相宇", Password = "11111", Question = "《悲伤恋歌》", UserID = 6, UserName = "admin" });
            userList.Add(new User { Answer = "我的梦想?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "成为亿万富翁", UserID = 7, UserName = "llj" });
            userList.Add(new User { Answer = "我最喜欢事?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "编程", UserID = 8, UserName = "admin" });
            userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 9, UserName = "admin" });
            userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 10, UserName = "admin" });
            userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 11, UserName = "admin" });
            userList.Add(new User { Answer = "我最喜欢事?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "编程", UserID = 12, UserName = "admin" });
            userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 13, UserName = "admin" });
            userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 14, UserName = "admin" });
            userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 15, UserName = "admin" });
            return userList;
        }
    }
}

 

注:其中的一部分主要属性已在左侧列出,并且点击按钮就能看到它的效果

  DataForm是用来显示所选中的数据

  DataPager用来分页,其中的PageSize是每页显示的条数。

  此DataGrid自带数据有效性验证

  源代码下载:https://files.cnblogs.com/salam/Silverlight.Common.rar

posted @ 2010-07-12 16:31  ForrestWoo  阅读(3475)  评论(1编辑  收藏  举报