Click here to Skip to main content
15,031,872 members
Articles / Desktop Programming / WPF
Article
Posted 11 Dec 2017

Tagged as

Stats

4.9K views

Mixing WinForm ListView with WPF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
11 Dec 2017CPOL2 min read
Grouping and sorting in a WinForm listview fails in a WPF application unless Visual Styles are enabled.

Introduction

In a current project, I was tasked to add a WinForms project from an old application into a new WPF application I was building. No real problem, right? Wrong!

In the legacy project's window was a listview, set up with column sorting and automatic grouping when the user clicks on the column header. In the old project, sorting and grouping worked beautifully! Attached to the new application, neither sorting nor grouping worked.

What went wrong??

Background

The legacy application generated several reports, most of which were no longer considered useful. The decision was made to create a new application which, among other things would generate new, useful reports. Once completed, the legacy application would be discarded.

You can guess what happened — people complained that some of the old reports WERE useful! Fortunately, they were all located in one VisualStudio project.

I added that project to the new WPF application and added the necessary linkages. Voilà! I could access the reports.

The fly in the ointment – grouping and sorting did not work.

I did an Internet search and found several articles which told me what to do, but they were written for C# programmers. In C3, the "Main" is accessible and can be easily modified by the programmer. Just add

VB
Application.EnableVisualStyles();

to the Main routine. Alas! My employer says we use Visual Basic. The Microsoft article said to add:

VB
<System.STAThread()> _
Public Shared Sub Main()

but this only resulted in an error message complaining about two Main routines.

Solution

The Microsoft article also said that visual styles must be enabled before any windows of the application are opened. This led me to think about the application initialization sequence in Visual Basic. The "New" subroutine uses the "InitializeComponent()" to instantiate the components of the window, including the window itself! Therefore, I added the following New subroutine to MainWindow.xaml.vb:

VB
Public Sub New()

         ' This line runs before Windows objects are instantiated.
     System.Windows.Forms.Application.EnableVisualStyles()

     ' This call is required by the designer.
     InitializeComponent()

     ' Add any initialization after the InitializeComponent() call.

End Sub  ' New

Sorting and grouping now works as expected!

History

  • 2017-12-08: Original version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jalapeno Bob
Software Developer (Senior) S & D Acres
United States United States
BSEE 1972, Polytechnic Institute of Brooklyn, Brooklyn, NY
MSCS 1978, Stevens Institute, Hoboken, NJ

Now, I live in Texas with my wife and children and raise non-dairy (aka "meat") goats and chickens for fun and (maybe next year) profit.

As a hobby (we all need one, don't we?? Smile | :) ), I write agriculture related software.

Comments and Discussions

 
-- There are no messages in this forum --