You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Setting ListViewGroup.HorizontalAlignment does not work once handle is created #2554
Setting listView1.Groups[0].HeaderAlignment = HorizontalAlignment.Left; causes no update to the alignment of the group.
However, calling 'RecreateHandleon theListView` does fix this.
The fix would be make the setter of ListViewGroup.HeaderAlignment recreate the handle: i.e.
/// <summary>/// The alignment of the group header./// </summary>[DefaultValue(HorizontalAlignment.Left)][SRCategory(nameof(SR.CatAppearance))]publicHorizontalAlignmentHeaderAlignment{get=> _headerAlignment;set{if(!ClientUtils.IsEnumValid(value,(int)value,(int)HorizontalAlignment.Left,(int)HorizontalAlignment.Center)){thrownewInvalidEnumArgumentException(nameof(value),(int)value,typeof(HorizontalAlignment));}if(_headerAlignment!=value){
_headerAlignment = value;ListView?.RecreateHandleInternal();}}}
The fix would be make the setter of ListViewGroup.HeaderAlignment recreate the handle
I didn't inspect this scenario but recreating handles has risk of flickering (maybe not noticeable on normal machines, but low end machines and remote connections may observe it) and causing issues with focus/accessibility/etc. so this suggestion by default sounds wrong to me and should only be chosen as a last resort (with explicitely testing for above issues) if there is no Win32 API to update whatever stores this property on the native side.
Setting listView1.Groups[0].HeaderAlignment = HorizontalAlignment.Left; causes no update to the alignment of the group.
UpdateGroupNative is not checking the return value of LVM_SETGROUPINFO, when I'm stepping through in the debugger I'm getting -1 for the update, according to the doc this means the update was not processed.
If I manually change the group ID in the LVGROUP struct in the debugger (but not in the SendMessageW call - effectively renaming the group to a different ID) then I'm getting the old group ID as return value of the message and the control correctly updates itself showing the changed alignment. (Of course now the group has a different ID, so if I want to change the ID back to the original value I have to send another message.)
I don't have time to try other combinations of settings in the LVGROUP struct, but it is very strange that LVM_SETGROUPINFO fails unless the group ID is changed. I wonder if its purpose is underdocumented and/or its implementation has bugs.
Even if further investigation doesn't yield new insights it may be a better workaround to rename the group twice instead of recreating the whole ListView.
internalvoidUpdateGroupNative(ListViewGroupgroup){Debug.Assert(IsHandleCreated,"UpdateGroupNative precondition: list-view handle must be created");varlvgroup=newLVGROUP();try{lvgroup=GetLVGROUP(group);intretval=(int)User32.SendMessageW(this,(User32.WindowMessage)LVM.SETGROUPINFO,(IntPtr)group.ID,reflvgroup);}finally{DestroyLVGROUP(lvgroup);}// The comctl32 ListView does not correctly invalidate itself, so we need to invalidate the entire ListViewInvalidate();}
Changing the caption (or alignment) of a group follows the same philosophy except that you have to use a workaround because of a bug in the LVM_SETGROUPINFO message / ListView_SetGroupInfo() macro. The bug (unless Microsoft defines it as a feature) prevents you from changing the caption if you don't change the identifier of the group.
Hopefully there is a workaround that requires two steps. First you change the caption and the identifier and then you change back the identifier to its original value (see Listing 5).
Maybe we could avoid recreating the handle in Header setting too!
Thanks for the link, not being able to update the group text without changing the ID is silly and made me think some more about what I observed, I think I now understand why this happens. Including LVGF_GROUPID in LVGROUP.mask when calling LVM_SETGROUPINFO probably asks to rename the group, I suspect if the group ID to which you want to rename it already exists within the ListView this fails (so always fails when renaming to your own ID) - removing the LVGF_GROUPID flag when doing updates seems to do the trick:
Activity
hughbe commentedon Dec 22, 2019
This also repros in .NET Framework
weltkante commentedon Jan 2, 2020
I didn't inspect this scenario but recreating handles has risk of flickering (maybe not noticeable on normal machines, but low end machines and remote connections may observe it) and causing issues with focus/accessibility/etc. so this suggestion by default sounds wrong to me and should only be chosen as a last resort (with explicitely testing for above issues) if there is no Win32 API to update whatever stores this property on the native side.
hughbe commentedon Jan 2, 2020
I did notice flickering on my Remote Desktop actually when testing the fix.
The problem according to my investigation appears to be that calling LVM_SETGROUPINFO and changing the uAlign field (https://docs.microsoft.com/en-us/windows/win32/controls/lvm-setgroupinfo) doesn’t update the group alignment.
This problem is the same for ListViewGroup.Header, which explicitly calls
RecreateHandlewhich is why I adopted this approachweltkante commentedon Jan 2, 2020
I debugged this and something fishy is going on
UpdateGroupNative is not checking the return value of
LVM_SETGROUPINFO, when I'm stepping through in the debugger I'm getting-1for the update, according to the doc this means the update was not processed.If I manually change the group ID in the
LVGROUPstruct in the debugger (but not in theSendMessageWcall - effectively renaming the group to a different ID) then I'm getting the old group ID as return value of the message and the control correctly updates itself showing the changed alignment. (Of course now the group has a different ID, so if I want to change the ID back to the original value I have to send another message.)I don't have time to try other combinations of settings in the
LVGROUPstruct, but it is very strange thatLVM_SETGROUPINFOfails unless the group ID is changed. I wonder if its purpose is underdocumented and/or its implementation has bugs.Even if further investigation doesn't yield new insights it may be a better workaround to rename the group twice instead of recreating the whole ListView.
hughbe commentedon Jan 2, 2020
Noting current implementation:
I shall experiment :)
hughbe commentedon Jan 2, 2020
http://users.skynet.be/oleole/Listview_grouping_feature.htm
Maybe we could avoid recreating the handle in Header setting too!
weltkante commentedon Jan 2, 2020
Thanks for the link, not being able to update the group text without changing the ID is silly and made me think some more about what I observed, I think I now understand why this happens. Including
LVGF_GROUPIDinLVGROUP.maskwhen callingLVM_SETGROUPINFOprobably asks to rename the group, I suspect if the group ID to which you want to rename it already exists within the ListView this fails (so always fails when renaming to your own ID) - removing theLVGF_GROUPIDflag when doing updates seems to do the trick:So probably just a case of underdocumented functionality.
Update LVM_SETGROUPINFO docs
RussKie commentedon Jan 2, 2020
You're both awesome! 💯
Update LVM_SETGROUPINFO docs