Enable groups in the ListView hosted by WPF

65 views Asked by At

There is an old Winforms control which I hosted in WPF app. Control makes use of System.Windows.Forms.ListView inside and this ListView uses Groups feature.

The problem is that this control when hosted by WPF does not show groups. I've manually compared properties of ListView when it's hosted by Winforms app and WPF app. For both ListViews ShowGroups property is true.

However there is a property called GroupsEnabled and it's true when control is hosted in Winforms and false when it's hosted in WPF. I've found definition here:

internal bool GroupsEnabled
{ 
    get { 
        return this.ShowGroups && groups != null && groups.Count > 0 && ComctlSupportsVisualStyles && !VirtualMode;
    } 
}

VirtualMode is false for both but ComctlSupportsVisualStyles is true for Winforms hosting and false for WPF app.
Code of ComctlSupportsVisualStyles from the same source:

private bool ComctlSupportsVisualStyles { 
 get {
      if(!listViewState[LISTVIEWSTATE_comctlSupportsVisualStylesTested])  
      {
          listViewState[LISTVIEWSTATE_comctlSupportsVisualStylesTested] = true;
          listViewState[LISTVIEWSTATE_comctlSupportsVisualStyles] = Application.ComCtlSupportsVisualStyles; 
      }
      return listViewState[LISTVIEWSTATE_comctlSupportsVisualStyles]; 
  } 
}

I think I need to set Application.ComCtlSupportsVisualStyles somehow in my WPF code.
And this must be System.Windows.Forms.Application and not System.Windows.Application.
Is there any way to do it?

1

There are 1 answers

0
user8700382 On

Enabling visual styles for your application should fix the problem:

System.Windows.Forms.Application.EnableVisualStyles();

The WinForms ListView control does not support groups unless visual styles are enabled (technically, unless version 6 of the ComCtrl32 library is used, which is the same version as required to support visual styles).

See also: How to: Enable Visual Styles in a Hybrid Application