【问题标题】:add tooltip to datagrid column filled from sqlite query将工具提示添加到从 sqlite 查询填充的数据网格列
【发布时间】:2018-05-10 13:00:43
【问题描述】:

我正在尝试向可能存在或不存在的特定列添加工具提示,具体取决于 sqlite 查询的结果。列标题是根据查询结果填写的。

我有这个代码来填充数据网格:

SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataAdapter sda = new SQLiteDataAdapter(command);
sda.Fill(dt);
MainDataGrid.ItemsSource = dt.DefaultView;

我想做这样的事情:

if(ColumnHeader == "Gauge")
{Column.Tooltip.Text = "Double click Gauge # to view details";} //Tooltip shows for entire column

但是,我无法想象这会这么容易。我完全是在 c# 和 wpf 中自学的,所以我的很多问题都来自于不知道如何正确表达问题。 我不是在寻找复制粘贴的答案。只是一个链接和一个解释。

【问题讨论】:

  • Here is duplicate 如果AutoGenerateColumns="False"。无论哪种情况,您都可以简单地枚举MainDataGrid.Columns。

标签: c# wpf sqlite datagrid


【解决方案1】:

我使用了以下代码:

void MainDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        string tooltip = null;

        switch (e.Column.Header.ToString())
        {
            case "Gauge":
                tooltip = "Double click Gauge ID to view its details";
                break;
        }

        if (tooltip != null)
        {
            var style = new Style(typeof(DataGridCell));
            style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, tooltip));
            e.Column.CellStyle = style;
        }
    }

我在这里找到了:Setting ToolTip for DataGridView automatically created columns

感谢 @Sinatr 让我走上正确的道路。

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多