在数据网格中的特定列上显示工具提示

时间:2014-04-10 21:14:49

标签: c# wpf datagrid tooltip

我目前正在开发一个C#WPF项目,我以编程方式创建数据集并将其绑定到数据网格。

程序正在读取日志文件,并将日志文件的每一行处理为数据网格中的各个列。

根据正在查看的日志类型,数据网格可以具有不同类型的列。

其中一列是错误代码,我想要实现的是当用户将鼠标悬停在状态代码上时,将显示工具提示,显示状态代码的含义。

例如,如果列中的状态代码为404,则工具提示会显示' 404错误表示无法找到页面'

以下是我如何创建数据集和绑定到datagrid的代码

public void processAccessLog(System.Windows.Controls.UserControl userControl, System.Windows.Controls.DataGrid apacheDataGrid)
{
    string client;
    string remoteUser;
    string date;
    string requestLine;
    string statusCode;
    string clientSizeReturned;
    string referer;
    string userAgent;

    using (StreamReader reader = new StreamReader(ApacheFilePath))
    {
        string line;
        DataSet ds = new DataSet();
        DataTable table = new DataTable();
        DataColumn clientCol = new DataColumn("Client IP", typeof(string));
        DataColumn remoteUserCol = new DataColumn("Remote User", typeof(string));
        DataColumn dateCol = new DataColumn("Date", typeof(string));
        DataColumn requestLineCol = new DataColumn("Request Line", typeof(string));
        DataColumn statusCodeCol = new DataColumn("Status Code", typeof(string));
        DataColumn clientSizeReturnedCol = new DataColumn("Client Size Returned", typeof(string));
        DataColumn refererCol = new DataColumn("Referer", typeof(string));
        DataColumn userAgentCol = new DataColumn("User Agent", typeof(string));
        //DataColumn messageCol = new DataColumn("Message", typeof(string));

        table.Columns.Add(clientCol);
        table.Columns.Add(remoteUserCol);
        table.Columns.Add(dateCol);
        table.Columns.Add(requestLineCol);
        table.Columns.Add(statusCodeCol);
        table.Columns.Add(clientSizeReturnedCol);
        table.Columns.Add(refererCol);
        table.Columns.Add(userAgentCol);
        ds.Tables.Add(table);

        while ((line = reader.ReadLine()) != null)
        {
            client = line.Substring(0, line.IndexOf(' '));
            int startOfRemoteUser = client.Length + 1;
            remoteUser = line.Substring(startOfRemoteUser, CommonTasks.getIndexOfSpaceCharAfterIndex(startOfRemoteUser, line) - client.Length);
            int startOfDate = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfRemoteUser, '[', line) + 1;
            date = line.Substring(startOfDate, CommonTasks.getIndexOfCharacterAfterStartPosition(startOfDate, ']', line) - startOfDate);
            int startOfRequestLine = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfDate, '"', line) + 1;
            requestLine = line.Substring(startOfRequestLine, CommonTasks.getIndexOfCharacterAfterStartPosition(startOfRequestLine, '"', line) - startOfRequestLine);
            int startOfStatusCode = line.IndexOf(' ', startOfRequestLine + requestLine.Length) + 1;
            statusCode = line.Substring(startOfStatusCode, line.IndexOf(' ', startOfStatusCode) - startOfStatusCode);
            int startOfSizeReturned = line.IndexOf(' ', startOfStatusCode + statusCode.Length) + 1;
            clientSizeReturned = line.Substring(startOfSizeReturned, line.IndexOf(' ', startOfSizeReturned) - startOfSizeReturned);
            int startOfReferer = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfSizeReturned + clientSizeReturned.Length, '"', line) + 1;
            referer = line.Substring(startOfReferer, CommonTasks.getIndexOfCharacterAfterStartPosition(startOfReferer, '"', line)- startOfReferer);
            int startOfUserAgent = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfReferer + referer.Length + 1, '"', line) + 1;
            userAgent = line.Substring(startOfUserAgent).Replace("\"", "");
            DataRow row = table.NewRow();
            row[clientCol] = client;
            row[remoteUserCol] = remoteUser;
            row[dateCol] = date;
            row[requestLineCol] = requestLine;
            row[statusCodeCol] = statusCode;
            row[clientSizeReturnedCol] = clientSizeReturned;
            row[refererCol] = referer;
            row[userAgentCol] = userAgent;
            table.Rows.Add(row);
        }
        userControl.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
        {
            apacheDataGrid.ItemsSource = ds.Tables[0].DefaultView;
            return null;
        }), null);  
    }

感谢您提供的任何帮助

0 个答案:

没有答案