Sliverlight学习笔记七DataGrid控件

本文介绍了一个使用DataGrid展示用户列表的例子,包括XAML布局、数据绑定、分组及分页功能实现。提供了完整的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一节是DataGrid,绑定数据源

效果如下图:

2010071216210853.png

 

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自带数据有效性验证

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

基于数据挖掘的音乐推荐系统设计与实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐与其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐与其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值