简体   繁体   中英

Enable groups in the ListView hosted by WPF

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM