如何设置和保持最小宽度?

时间:2021-11-22 07:13:37

I use some wx.ListCtrl classes in wx.LC_REPORT mode, augmented with ListCtrlAutoWidthMixin.

我在wx.LC_REPORT模式下使用了一些wx.ListCtrl类,并使用ListCtrlAutoWidthMixin进行了扩充。

The problem is: When user double clicks the column divider (to auto resize column), column width is set to match the width of contents. This is done by the wx library and resizes column to just few pixels when the control is empty.

问题是:当用户双击列分隔符(自动调整大小列)时,列宽设置为匹配内容的宽度。这是由wx库完成的,当控件为空时,将列重新调整为几个像素。

I tried calling

我试着打电话

self.SetColumnWidth(colNumber, wx.LIST_AUTOSIZE_USEHEADER)

while creating the list, but it just sets the initial column width, not the minimum allowed width.

在创建列表时,它只是设置初始列宽,而不是最小允许宽度。

Anyone succeeded with setting column minimal width?

任何人都成功设置了列最小宽度?

EDIT: Tried catching

编辑:尝试抓住

 wx.EVT_LEFT_DCLICK 

with no success. This event isn't generated when user double clicks column divider. Also tried with

没有成功。当用户双击列分隔符时,不会生成此事件。也尝试过

wx.EVT_LIST_COL_END_DRAG 

this event is generated, usually twice, for double click, but I don't see how can I retrieve information about new size, or how to differentiate double click from drag&drop. Anyone has some other ideas?

生成此事件,通常是两次,双击,但我不知道如何检索有关新大小的信息,或如何区分双击与拖放。有没有其他想法?

2 个解决方案

#1


Honestly, I've stopped using the native wx.ListCtrl in favor of using ObjectListView. There is a little bit of a learning curve, but there are lots of examples. This would be of interest to your question.

老实说,我已经停止使用本机wx.ListCtrl而支持使用ObjectListView。有一点学习曲线,但有很多例子。这对你的问题很感兴趣。

#2


Ok, after some struggle I got working workaround for that. It is ugly from design point of view, but works well enough for me.

好吧,经过一番努力,我得到了解决方法。从设计的角度看它很难看,但对我来说效果还不错。

That's how it works:

它是如何工作的:

  1. Store the initial width of column.

    存储列的初始宽度。

    self.SetColumnWidth(colNum, wx.LIST_AUTOSIZE_USEHEADER)
    
    self.__columnWidth[colNum] = self.GetColumnWidth(c)
    
  2. Register handler for update UI event.

    注册更新UI事件的处理程序。

    wx.EVT_UPDATE_UI(self, self.GetId(), self.onUpdateUI)
    
  3. And write the handler function.

    并编写处理函数。

    def onUpdateUI(self, evt):
        for colNum in xrange(0, self.GetColumnCount()-1):
            if self.GetColumnWidth(colNum) < self.__columnWidth[colNum]:
                self.SetColumnWidth(colNum, self.__columnWidth[colNum])
        evt.Skip()
    

The self.GetColumnCount() - 1 is intentional, so the last column is not resized. I know this is not an elegant solution, but it works well enough for me - you can not make columns too small by double clicking on dividers (in fact - you can't do it at all) and double-clicking on the divider after last column resizes the last column to fit list control width.

self.GetColumnCount() - 1是故意的,因此最后一列不会调整大小。我知道这不是一个优雅的解决方案,但它对我来说效果很好 - 你不能通过双击分隔器使列太小(事实上 - 你完全不能这样做)并且双击分隔符之后最后一列调整最后一列的大小以适合列表控件宽度。

Still, if anyone knows better solution please post it.

不过,如果有人知道更好的解决方案,请发布。

#1


Honestly, I've stopped using the native wx.ListCtrl in favor of using ObjectListView. There is a little bit of a learning curve, but there are lots of examples. This would be of interest to your question.

老实说,我已经停止使用本机wx.ListCtrl而支持使用ObjectListView。有一点学习曲线,但有很多例子。这对你的问题很感兴趣。

#2


Ok, after some struggle I got working workaround for that. It is ugly from design point of view, but works well enough for me.

好吧,经过一番努力,我得到了解决方法。从设计的角度看它很难看,但对我来说效果还不错。

That's how it works:

它是如何工作的:

  1. Store the initial width of column.

    存储列的初始宽度。

    self.SetColumnWidth(colNum, wx.LIST_AUTOSIZE_USEHEADER)
    
    self.__columnWidth[colNum] = self.GetColumnWidth(c)
    
  2. Register handler for update UI event.

    注册更新UI事件的处理程序。

    wx.EVT_UPDATE_UI(self, self.GetId(), self.onUpdateUI)
    
  3. And write the handler function.

    并编写处理函数。

    def onUpdateUI(self, evt):
        for colNum in xrange(0, self.GetColumnCount()-1):
            if self.GetColumnWidth(colNum) < self.__columnWidth[colNum]:
                self.SetColumnWidth(colNum, self.__columnWidth[colNum])
        evt.Skip()
    

The self.GetColumnCount() - 1 is intentional, so the last column is not resized. I know this is not an elegant solution, but it works well enough for me - you can not make columns too small by double clicking on dividers (in fact - you can't do it at all) and double-clicking on the divider after last column resizes the last column to fit list control width.

self.GetColumnCount() - 1是故意的,因此最后一列不会调整大小。我知道这不是一个优雅的解决方案,但它对我来说效果很好 - 你不能通过双击分隔器使列太小(事实上 - 你完全不能这样做)并且双击分隔符之后最后一列调整最后一列的大小以适合列表控件宽度。

Still, if anyone knows better solution please post it.

不过,如果有人知道更好的解决方案,请发布。