如何在 wxGrid 中禁用多重选择?

发布于 2025-01-08 05:18:24 字数 107 浏览 1 评论 0原文

我只想选择 wxGrid 中的一行。 是否可以在 wxGrid 中禁用多重选择? 我尝试了 SetSelectionMode,它可以帮助我仅选择行。但我不能将它用于单行选择。

I want to select only one row in wxGrid.
Is is possible to disable multiselection in a wxGrid?
I tried SetSelectionMode, it helps me to select only rows. But I cannot use it for single row selection.

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

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

发布评论

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

评论(3

涫野音 2025-01-15 05:18:24

捕获 wxEVT_GRID_SELECT_CELL 事件。

使用 GetSelectedRows() 获取选定行的数组。

如果选择了多行 -

  • 使用 SelectRow() 取消选择除数组中的第一行之外的所有内容。

Capture the wxEVT_GRID_SELECT_CELL event.

Use GetSelectedRows() to obtain an array of selected rows.

If there are more than one row selected -

  • Use SelectRow() to deselect everything except, say, the first row in the array.
得不到的就毁灭 2025-01-15 05:18:24
    wx.EVT_MOTION(self.grid.GetGridWindow(), self.OnMouseMotion)
    wx.EVT_LEFT_DOWN(self.grid.GetGridWindow(), self.OnMouseLeftDown)
    wx.EVT_KEY_DOWN(self.grid.GetGridWindow(), self.OnKeyPress)

def OnMouseMotion(self,event):
    if event.Dragging():
        return
    else: 
        event.Skip()

def OnMouseLeftDown(self,event):
    if event.AltDown() or event.ShiftDown() or event.ControlDown():
        return
    else:
        event.Skip()

def OnKeyPress(self,event):
    if event.GetKeyCode() in [wx.WXK_SHIFT,wx.WXK_ALT,wx.WXK_CONTROL,wx.WXK_UP,wx.WXK_DOWN,]:
        return
    else:
        event.Skip()
    wx.EVT_MOTION(self.grid.GetGridWindow(), self.OnMouseMotion)
    wx.EVT_LEFT_DOWN(self.grid.GetGridWindow(), self.OnMouseLeftDown)
    wx.EVT_KEY_DOWN(self.grid.GetGridWindow(), self.OnKeyPress)

def OnMouseMotion(self,event):
    if event.Dragging():
        return
    else: 
        event.Skip()

def OnMouseLeftDown(self,event):
    if event.AltDown() or event.ShiftDown() or event.ControlDown():
        return
    else:
        event.Skip()

def OnKeyPress(self,event):
    if event.GetKeyCode() in [wx.WXK_SHIFT,wx.WXK_ALT,wx.WXK_CONTROL,wx.WXK_UP,wx.WXK_DOWN,]:
        return
    else:
        event.Skip()
倾城泪 2025-01-15 05:18:24

我发现这个问题很老了,但互联网上没有太多关于这个论点的内容。

互联网上发布的许多解决方案尚未完成或存在一些怪癖。
这是我的解决方案。我有一个继承 wxGrid 的类,具有以下方法

void ReportGrid::DisableRangeHandler (wxGridRangeSelectEvent &ev)
{
    if( ev.Selecting() && (ev.GetTopRow() != ev.GetBottomRow()) ) {
        ev.StopPropagation();
        ev.Veto();
    }
}

void ReportGrid::DisableRangeSelectCellHandler (wxGridEvent &ev)
{
    SelectRow(ev.GetRow());
}

void ReportGrid::DisableCtrlMaiusHandler (wxGridEvent &ev)
{
    if (ev.ControlDown() || ev.ShiftDown()){
        ev.Veto();
    } else {
        ev.Skip();
    }
}

void ReportGrid::DisableDraggingHandler (wxMouseEvent &ev)
{
    if (ev.Dragging()){
        ev.Skip(false);
    }
}

void ReportGrid::SingleSelectionMode(bool enabled)
{
    if (_singleSelection == enabled){
        return;
    }
    if (enabled){
        Bind(wxEVT_GRID_RANGE_SELECT, &ReportGrid::DisableRangeHandler, this);
        Bind(wxEVT_GRID_SELECT_CELL, &ReportGrid::DisableRangeSelectCellHandler, this);
        Bind(wxEVT_GRID_CELL_LEFT_CLICK, &ReportGrid::DisableCtrlMaiusHandler, this);
        GetGridWindow()->Bind(wxEVT_MOTION, &ReportGrid::DisableDraggingHandler, this);
        _singleSelection = true;
    } else {
        Unbind(wxEVT_GRID_RANGE_SELECT, &ReportGrid::DisableRangeHandler, this);
        Unbind(wxEVT_GRID_SELECT_CELL, &ReportGrid::DisableRangeSelectCellHandler, this);
        Unbind(wxEVT_GRID_CELL_LEFT_CLICK, &ReportGrid::DisableCtrlMaiusHandler, this);
        GetGridWindow()->Unbind(wxEVT_MOTION, &ReportGrid::DisableDraggingHandler, this);
        _singleSelection = false;
    }
}

这是我在 wxWidgets 上最接近的单行选择。

I've seen that this question is quite old but there not so much on this argument in internet.

Many of the solution posted on internet are not completed or with some quirks.
Here's my solution. I've a class that inherit wxGrid, with the following methods

void ReportGrid::DisableRangeHandler (wxGridRangeSelectEvent &ev)
{
    if( ev.Selecting() && (ev.GetTopRow() != ev.GetBottomRow()) ) {
        ev.StopPropagation();
        ev.Veto();
    }
}

void ReportGrid::DisableRangeSelectCellHandler (wxGridEvent &ev)
{
    SelectRow(ev.GetRow());
}

void ReportGrid::DisableCtrlMaiusHandler (wxGridEvent &ev)
{
    if (ev.ControlDown() || ev.ShiftDown()){
        ev.Veto();
    } else {
        ev.Skip();
    }
}

void ReportGrid::DisableDraggingHandler (wxMouseEvent &ev)
{
    if (ev.Dragging()){
        ev.Skip(false);
    }
}

void ReportGrid::SingleSelectionMode(bool enabled)
{
    if (_singleSelection == enabled){
        return;
    }
    if (enabled){
        Bind(wxEVT_GRID_RANGE_SELECT, &ReportGrid::DisableRangeHandler, this);
        Bind(wxEVT_GRID_SELECT_CELL, &ReportGrid::DisableRangeSelectCellHandler, this);
        Bind(wxEVT_GRID_CELL_LEFT_CLICK, &ReportGrid::DisableCtrlMaiusHandler, this);
        GetGridWindow()->Bind(wxEVT_MOTION, &ReportGrid::DisableDraggingHandler, this);
        _singleSelection = true;
    } else {
        Unbind(wxEVT_GRID_RANGE_SELECT, &ReportGrid::DisableRangeHandler, this);
        Unbind(wxEVT_GRID_SELECT_CELL, &ReportGrid::DisableRangeSelectCellHandler, this);
        Unbind(wxEVT_GRID_CELL_LEFT_CLICK, &ReportGrid::DisableCtrlMaiusHandler, this);
        GetGridWindow()->Unbind(wxEVT_MOTION, &ReportGrid::DisableDraggingHandler, this);
        _singleSelection = false;
    }
}

This is the closest i've been to have a single row selection on wxWidgets.

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