微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight Datagrid 行上增加ToolTip

1) 前台代码

<UserControl xmlns:data="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightApplication131.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">


    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid Name="TestDataGrid" AutoGenerateColumns="False">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="ClassID" Binding="{Binding ClassID}"></data:DataGridTextColumn>
                <data:DataGridTextColumn Header="ClassName" Binding="{Binding ClassName}"></data:DataGridTextColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
</UserControl>

2)后台代码

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 SilverlightApplication131
{
    public partial class MainPage : UserControl
    {
        private List<ClassDemo> classItems = null;
        private List<StudentDemo> studentItems = null;
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            TestDataGrid.LoadingRow += new EventHandler<DataGridRowEventArgs>(TestDataGrid_LoadingRow);
        }
        /// <summary>
        /// 获取班级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainPage_Loaded(object sender,RoutedEventArgs e)
        {
            GetClassDemo();
            GetStudentDemo();
            TestDataGrid.ItemsSource = classItems;
        }
        private void GetClassDemo()
        {
            classItems = new List<ClassDemo>();
            classItems.Add(new ClassDemo(1,"一班"));
            classItems.Add(new ClassDemo(2,"二班"));
            classItems.Add(new ClassDemo(3,"三班"));
            classItems.Add(new ClassDemo(4,"四班"));
        }
        /// <summary>
        /// 获取学生
        /// </summary>
        private void GetStudentDemo()
        {
            studentItems = new List<StudentDemo>();
            studentItems.Add(new StudentDemo(1,"张1",1));
            studentItems.Add(new StudentDemo(2,"张2",2));
            studentItems.Add(new StudentDemo(3,"张3",2));
            studentItems.Add(new StudentDemo(4,"张4",3));
            studentItems.Add(new StudentDemo(5,"张5",3));
            studentItems.Add(new StudentDemo(6,"张6",3));
            studentItems.Add(new StudentDemo(7,"张7",4));
        }
        /// <summary>
        /// 在加载DataGridRow时设置ToolTip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TestDataGrid_LoadingRow(object sender,DataGridRowEventArgs e)
        {
            ClassDemo item = e.Row.DataContext as ClassDemo;
            ToolTipService.SetToolTip(e.Row,GenerateStackPanel(item));
        }
        /// <summary>
        /// 生成StackPanel
        /// </summary>
        /// <param name="classID"></param>
        /// <returns></returns>
        private StackPanel GenerateStackPanel(ClassDemo classItem)
        {
            StackPanel stackPanel = new StackPanel();
            stackPanel.Orientation = Orientation.Vertical;
            IEnumerable<StudentDemo> items = studentItems.Where(u => u.ClassID == classItem.ClassID);
            //显示班级信息
            TextBlock classtextBlock = new TextBlock();
            classtextBlock.Foreground = new SolidColorBrush(Colors.Red);
            classtextBlock.Text = classItem.ClassName;
            stackPanel.Children.Add(classtextBlock);
            //显示学生信息
            if (items.Count() > 0)
            {
                foreach (StudentDemo item in items)
                {
                    TextBlock studentTextBlock = new TextBlock();
                    studentTextBlock.Text = item.StudentName;
                    stackPanel.Children.Add(studentTextBlock);
                }
            }
            return stackPanel;
        }
    }
    /// <summary>
    /// 学生类
    /// </summary>
    public class StudentDemo
    {
        public StudentDemo() { }
        public StudentDemo(int _studentID,string _studentName,int _classID)
        {
            this.StudentID = _studentID;
            this.StudentName = _studentName;
            this.ClassID = _classID;
        }
        private int studentID;


        public int StudentID
        {
            get { return studentID; }
            set { studentID = value; }
        }
        private string studentName;


        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        private int classID;


        public int ClassID
        {
            get { return classID; }
            set { classID = value; }
        }
    }
    /// <summary>
    /// 班级类
    /// </summary>
    public class ClassDemo
    {
        public ClassDemo() { }
        public ClassDemo(int _classID,string _className)
        {
            this.ClassID = _classID;
            this.ClassName = _className;
        }
        private int classID;


        public int ClassID
        {
            get { return classID; }
            set { classID = value; }
        }
        private string className;


        public string ClassName
        {
            get { return className; }
            set { className = value; }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐