Skip to content

Setting ListViewGroup.HorizontalAlignment does not work once handle is created #2554

Closed

Description

@hughbe

E.g. with the following form
image

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))]
        public HorizontalAlignment HeaderAlignment
        {
            get => _headerAlignment;
            set
            {
                if (!ClientUtils.IsEnumValid(value, (int)value, (int)HorizontalAlignment.Left, (int)HorizontalAlignment.Center))
                {
                    throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(HorizontalAlignment));
                }

                if (_headerAlignment != value)
                {
                    _headerAlignment = value;
                    ListView?.RecreateHandleInternal();
                }
            }
        }

Screen Recording 2019-12-22 at 06 35 pm

Activity

  1. hughbe commented on Dec 22, 2019

    @hughbe
    ContributorAuthor

    This also repros in .NET Framework

  2. ghost added
    🚧 work in progress
    on Dec 22, 2019
  3. added
    🪲 bug
    on Dec 29, 2019
  4. added this to the 5.0 milestone on Dec 29, 2019
  5. weltkante commented on Jan 2, 2020

    @weltkante
    Contributor

    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.

  6. hughbe commented on Jan 2, 2020

    @hughbe
    ContributorAuthor

    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 RecreateHandle which is why I adopted this approach

  7. weltkante commented on Jan 2, 2020

    @weltkante
    Contributor

    I debugged this and something fishy is going on

    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.

  8. hughbe commented on Jan 2, 2020

    @hughbe
    ContributorAuthor

    Noting current implementation:

    internal void UpdateGroupNative(ListViewGroup group)
    {
        Debug.Assert(IsHandleCreated, "UpdateGroupNative precondition: list-view handle must be created");
    
        var lvgroup = new LVGROUP();
        try
        {
            lvgroup = GetLVGROUP(group);
            int retval = (int)User32.SendMessageW(this, (User32.WindowMessage)LVM.SETGROUPINFO, (IntPtr)group.ID, ref lvgroup);
        }
        finally
        {
            DestroyLVGROUP(lvgroup);
        }
    
        // The comctl32 ListView does not correctly invalidate itself, so we need to invalidate the entire ListView
        Invalidate();
    }

    I shall experiment :)

  9. hughbe commented on Jan 2, 2020

    @hughbe
    ContributorAuthor

    http://users.skynet.be/oleole/Listview_grouping_feature.htm

    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!

  10. weltkante commented on Jan 2, 2020

    @weltkante
    Contributor

    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:

    lvgroup = GetLVGROUP(group);
    lvgroup.mask &= ~ComCtl32.LVGF.GROUPID;
    int retval = (int)User32.SendMessageW(this, (User32.WindowMessage)LVM.SETGROUPINFO, (IntPtr)group.ID, ref lvgroup);

    So probably just a case of underdocumented functionality.

  11. added a commit that references this issue on Jan 2, 2020
  12. RussKie commented on Jan 2, 2020

    @RussKie
    Contributor

    You're both awesome! 💯

  13. added a commit that references this issue on Jan 4, 2020
  14. ghost removed
    🚧 work in progress
    on Jan 7, 2020
  15. removed this from the 5.0 Preview 1- 4 milestone on Apr 20, 2020
  16. ghost locked as resolved and limited conversation to collaborators on Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🪲 bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions