Question

I'm adding properties to an object of type CMFCPropertyGridCtrl like this:

myPropertyListCtrl.AddProperty(
    new CMFCPropertyGridProperty(
        _T("Name"),
        foo.GetName())
);

The result is that only the second column is visible but not the first that should contain "Name".

  • I found CMFCPropertyGridCtrl::GetPropertyColumnWidth() but there appears to be no corresponding Set... method...
  • I looked at the NewControls sample, in which the column sizing appears to be fully automatic. However, I couldn't find the relevant difference to my code.

What am I missing?

Was it helpful?

Solution

m_nLeftColumnWidth responsible for holding the "Name" column's width is a protected member of the CMFCPropertyGridCtrl class. Create your own class, that derives from CMFCPropertyGridCtrl and you will be able to manipulate m_nLeftColumnWidth.

OTHER TIPS

Note that m_nLeftColumnWidth is initially set to 0 in CMFCPropertyGridCtrl's ctor. One of the few other places that it is set, is in the OnSize() method (ref. AfxPropertyGridCtrl.cpp, line 2783 in VS2010), where it is set to half the width of the grid. This may not be ideal, nor the customized value described by overriding the class to explicitly set it, but may be good enough.

If so, then it is merely to trigger having the CMFCPropertyGridCtrl::OnSize() protected method. When used in a resizable window such as a CDockablePane, OnSize() will be called automatically. But in a CDialog, it needs to be trigger explicitly such as to send a WM_SIZE message:

CRect rect;
myPropGrid.GetWindowRect(&rect);
myPropGrid.PostMessage(WM_SIZE, 0, MAKELONG(rect.Width(),rect.Height()));
class CServerPropertyGridCtrl : public CMFCPropertyGridCtrl
{
public:
    void SetLeftColumnWidth(int cx)
    {
        m_nLeftColumnWidth = cx;
        AdjustLayout();
    }
};

The reason the "set" isn't there is because it's left to the header control. The below is the method of handling through MFC versus posting window messages:

HDITEM hdItem;
hdItem.mask = HDI_WIDTH; // indicating cxy is width
hdItem.cxy = 300; // whatever you want the property name column width to be
PropListCtrl.GetHeaderCtrl().SetItem(0, &hdItem);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top