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; }
        }
    }
}

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...