XtraGrid Suite - 有没有办法向单元格添加按钮或超链接?

发布于 2024-08-20 08:07:15 字数 408 浏览 8 评论 0原文

我正在使用 DevExpress 制作的 XtraGrid Suite。我找不到任何类型的功能来执行此操作,但我很好奇是否可以向网格单元添加按钮或超链接。

上下文:我有一个事件列表。每个事件都有时间、开始/结束和类别(实用程序和维护)。可以有开始事件和停止事件。完成对问题的分析后,我决定为每个事件设置 StartTime 和 EndTime 是行不通的。

因此,如果事件开始,我会将当前时间记录到事件对象,并将其设置为“开始”事件。我想向该行中的单元格添加“停止”按钮/超链接。如果用户希望记录结束事件,事件类型等将被复制到类型为“停止”的新事件,并且按钮将消失。

我希望这是有道理的。

编辑:Aaronaught 的答案实际上比我最初问的(按钮)更好,所以我更新了问题。这样,任何想要在单元格中放置超链接的人都可以从他的示例中受益:)

I'm working with the XtraGrid Suite made by DevExpress. I can't find any sort of functionality to do this, but I'm curious if you can add a button or hyperlink to a grid cell.

Context: I've got an Events list. Each Event has a Time, Start/End, and a Category (Utility and Maintenance). There can be Start events and Stop events. Having done my analysis of the problem, I've decided that having a StartTime and EndTime for each event would not work.

So if an event starts, I'd record the current time to the Event object, and set it as a 'Start' event. I'd like to add a "Stop" button/hyperlink to a cell in that row. If the user wishes to log an Ends event, the event type, etc would be copied to a new Event with the type 'Stop' and the button would disappear.

I hope this makes sense.

EDIT: Aaronaught's answer is actually better than what I was originally asking (a button) so I've updated the question. That way, anyone looking for putting a hyperlink in a cell can benefit from his example : )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窝囊感情。 2024-08-27 08:07:15

我更喜欢通过覆盖绘图代码并处理鼠标移动/单击事件来使用超链接样式文本,因为按钮不能很好地缩放到网格的典型行高度。但是,如果按钮确实是您想要的,那么您应该能够使用 RepositoryItemButtonEdit 作为编辑器类型来实现。

如果您对前者感兴趣,请发表评论,我将用一个示例来更新它。否则,如上所述,只需使用 RepositoryItemButtonEdit。如果需要,您可以更改其属性以占据整个单元格,然后将列设置为固定大小,以便按钮不会被拉伸。


更新:我在下面发布了一些“漂亮的超链接”的示例代码,我比标准的超链接单元更喜欢它,因为(a)它看起来更好,(b)它提供悬停反馈,并且 (c) 如果需要,您可以更改光标(我使用实用程序方法从操作系统获取本机手形光标,它比 Winforms 的内置手形光标具有更多的 3D 外观)。

非 DevExpress 用户阅读本文时请注意:我对标准 System.Windows.Forms.ListView 使用了几乎相同的技术。 Microsoft 在 Vista 和 Windows 7 中大量使用了这种 UI 模式,即使结果不是完美的复制品,学习如何使用也是很好的。

private int hoverRowHandle = GridControl.InvalidRowHandle;

private void gridView_Click(object sender, EventArgs e)
{
    if (hoverRowHandle != GridControl.InvalidRowHandle)
    {
        MyItem item = gridView.GetRow(hoverRowHandle) as MyItem;
        if (item != null)
            // Do whatever the "click" action is here
    }
}

private void gridView_CustomDrawCell(object sender,
    RowCellCustomDrawEventArgs e)
{
    if (e.Column == linkColumn)
    {
        bool hover = (hoverRowHandle == e.RowHandle);
        FontStyle style = hover ? FontStyle.Underline : FontStyle.Regular;
        TextFormatFlags formatFlags =
            TextFormatFlags.Left | TextFormatFlags.VerticalCenter |
            TextFormatFlags.WordEllipsis;
        Color foreColor = gridView.IsRowSelected(e.RowHandle) ?
            Color.White : (hover ? MyColors.LinkHover : MyColors.Link);
        using (Font font = new Font(gridControl.Font, style))
        {
            TextRenderer.DrawText(e.Graphics, "Link Text", font, e.Bounds,
                foreColor, formatFlags);
        }
        e.Handled = true;
    }
}

private void gridView_MouseLeave(object sender, EventArgs e)
{
    int tempRowHandle = hoverRowHandle;
    hoverRowHandle = GridControl.InvalidRowHandle;
    if (tempRowHandle != GridControl.InvalidRowHandle)
    {
        gridView.InvalidateRowCell(tempRowHandle, linkColumn);
    }
}

private void gridView_MouseMove(object sender, MouseEventArgs e)
{
    int tempRowHandle = hoverRowHandle;
    if (tempRowHandle != GridControl.InvalidRowHandle)
    {
        hoverRowHandle = GridControl.InvalidRowHandle;
        gridView.InvalidateRowCell(tempRowHandle, linkColumn);
    }
    GridHitInfo hitInfo = gridView.CalcHitInfo(e.Location);
    if (hitInfo.InRowCell && (hitInfo.Column == linkColumn))
    {
        hoverRowHandle = hitInfo.RowHandle;
        gridView.InvalidateRowCell(hoverRowHandle, linkColumn);
    }

    bool hoverDetail = (hoverRowHandle != GridControl.InvalidRowHandle);
    gridControl.Cursor = hoverDetail ? Cursors.Hand : Cursors.Default;
}

关于此代码的一些注意事项:

  • MyItem 是绑定到网格视图的任何类型的数据。也许它是一个 DataRow,或者如果数据源是 IList,它可能是某种自定义类型。

  • MyColors 是一个实用程序类,它定义了几个用于 UI 内容的public static readonly Color 字段。如果您只打算在一个网格上执行此操作,则可以用硬编码颜色替换对此的引用。

  • 我不费心缓存Font,尽管你可能可以,因为只有两个。

  • 光标逻辑会与您可能在网格中使用的任何其他光标逻辑混淆(对我来说几乎在所有情况下几乎都没有,所以通常应该没问题)。

  • 如果你想要有多个“链接列”,除了hoverRowHandle之外,你还需要维护一个hoverColumn状态字段,并且显然要改变那些单-列相等性比较以搜索多列。

对于我自己的 Winforms 应用程序,我实际上有一个扩展程序提供程序,它允许我通过放入列名称/链接文本对的列表来将此行为附加到 GridViewListView,但该代码有点太长,无法发布在这里。上面的例子应该可以帮助您入门。

I prefer to use hyperlink-style text by overriding the drawing code and handling mouse move/click events, as buttons don't scale too well to the typical row height of a grid. However, if a button is really what you want, you should be able to do that using the RepositoryItemButtonEdit as the editor type.

If you're interested in the former, leave a comment and I'll update this with an example. Otherwise, as mentioned, just use the RepositoryItemButtonEdit. You can change its properties to take up the whole cell if you want, and then make the column a fixed size so the button doesn't get stretched.


UPDATE: I'm posting some sample code for the "pretty hyperlink" below, which I like a lot better than a standard Hyperlink cell because (a) it looks nicer, (b) it gives hover feedback, and (c) you can change the cursor if you want (I use a utility method to get the native hand cursor from the OS, which has a more 3D look than Winforms' built-in hand).

Note to non-DevExpress users reading this: I use almost an identical technique for the standard System.Windows.Forms.ListView. Microsoft uses this UI pattern quite a bit in Vista and Windows 7 and it's good to learn how to do, even if the result isn't a perfect replica.

private int hoverRowHandle = GridControl.InvalidRowHandle;

private void gridView_Click(object sender, EventArgs e)
{
    if (hoverRowHandle != GridControl.InvalidRowHandle)
    {
        MyItem item = gridView.GetRow(hoverRowHandle) as MyItem;
        if (item != null)
            // Do whatever the "click" action is here
    }
}

private void gridView_CustomDrawCell(object sender,
    RowCellCustomDrawEventArgs e)
{
    if (e.Column == linkColumn)
    {
        bool hover = (hoverRowHandle == e.RowHandle);
        FontStyle style = hover ? FontStyle.Underline : FontStyle.Regular;
        TextFormatFlags formatFlags =
            TextFormatFlags.Left | TextFormatFlags.VerticalCenter |
            TextFormatFlags.WordEllipsis;
        Color foreColor = gridView.IsRowSelected(e.RowHandle) ?
            Color.White : (hover ? MyColors.LinkHover : MyColors.Link);
        using (Font font = new Font(gridControl.Font, style))
        {
            TextRenderer.DrawText(e.Graphics, "Link Text", font, e.Bounds,
                foreColor, formatFlags);
        }
        e.Handled = true;
    }
}

private void gridView_MouseLeave(object sender, EventArgs e)
{
    int tempRowHandle = hoverRowHandle;
    hoverRowHandle = GridControl.InvalidRowHandle;
    if (tempRowHandle != GridControl.InvalidRowHandle)
    {
        gridView.InvalidateRowCell(tempRowHandle, linkColumn);
    }
}

private void gridView_MouseMove(object sender, MouseEventArgs e)
{
    int tempRowHandle = hoverRowHandle;
    if (tempRowHandle != GridControl.InvalidRowHandle)
    {
        hoverRowHandle = GridControl.InvalidRowHandle;
        gridView.InvalidateRowCell(tempRowHandle, linkColumn);
    }
    GridHitInfo hitInfo = gridView.CalcHitInfo(e.Location);
    if (hitInfo.InRowCell && (hitInfo.Column == linkColumn))
    {
        hoverRowHandle = hitInfo.RowHandle;
        gridView.InvalidateRowCell(hoverRowHandle, linkColumn);
    }

    bool hoverDetail = (hoverRowHandle != GridControl.InvalidRowHandle);
    gridControl.Cursor = hoverDetail ? Cursors.Hand : Cursors.Default;
}

A few notes about this code:

  • MyItem is whatever type of data you have bound to the grid view. Maybe it's a DataRow, or maybe it's some custom type if the data source is an IList<T>.

  • MyColors is a utility class that defines a couple of public static readonly Color fields used for UI stuff. You can replace the references to that with hard-coded colours if you're only ever going to do this on one grid.

  • I don't bother caching the Font, although you probably could, since there are only two of them.

  • The cursor logic will mess with any other cursor logic you might use in the grid (which is virtually none in almost every case for me, so generally you should be fine).

  • If you want to have more than one "link column", you need to maintain a hoverColumn state field in addition to the hoverRowHandle, and obviously change those single-column equality comparisons to search for multiple columns.

For my own Winforms apps, I actually have an Extender Provider that lets me attach this behaviour to a GridView or ListView by tossing in a list of column name/link text pairs, but that code is just a wee bit too long to post here. The example above should get you started.

依 靠 2024-08-27 08:07:15

使用RepositoryItemButtonEdit并将TextEditStyle设置为HideTextEditor

Use the RepositoryItemButtonEdit and set the TextEditStyle to HideTextEditor.

獨角戲 2024-08-27 08:07:15

现在可以使用 RepositoryItemHyperLinkEdit 控件来实现这一点。

请参阅:RepositoryItemHyperLinkEdit 类

This can now be achieved using the RepositoryItemHyperLinkEdit control.

See: RepositoryItemHyperLinkEdit Class

梦行七里 2024-08-27 08:07:15

您可以使用 RepositoryItemButtonEdit :
选择您的目标列,在“属性”中单击“ColumnEdit”并选择“新建”,然后选择“ButtonEdit”。单击网格并选择“运行设计器”,在“存储库”组中选择“就地编辑器存储库”。选择“repositoryItemButtonEdit1”(如果未更改按钮编辑组件名称)选择“事件”选项卡并选择“ButtonPressed”事件。在此活动中填写您的代码。
如果需要,隐藏组件的编辑器部分,选择目标列,在“属性”中单击“ColumnEdit”找到“TextEditStyle”并选择“HideTextEditor”。

但是,有一个问题!?
我想在按钮中添加图片,有人有什么想法吗?

you can use RepositoryItemButtonEdit :
select your target column, in "Properties" click on "ColumnEdit" and select "new", after than select "ButtonEdit". click on your grid and select "run designer", select "in-place Editor Repository" on "Repository" group . select "repositoryItemButtonEdit1" (if you not changed Button edit component name) select "event" tab and select "ButtonPressed" event. fill your code in this event.
if you want, hide editor part of component, select target column, in "Properties" click on "ColumnEdit" find "TextEditStyle" and choose "HideTextEditor".

But, one question!??
i want add a picture into my button, somebody have any idea?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文