WinForms ListView TileSize when hosting in WPF - wpf

I am working on a WPF project using some WinForms user controls (to reuse code from other projects at my workplace). This works quite well by using the WindowsFormsHost control in WPF.
Recently I stumbled upon a strange case of how ListViews in Tile mode (ListView.View=Tile) are displayed when hosted in WPF. It seems like ListView.TileSize is ignored, and listview items are given a width of about 35-40 (instead of 200 which is tile size width in my test).
UserControl containing a ListView, hosted in a WPF application:
The same UserControl, running in a WinForms application:
Any thoughts for a solution to this problem?

Related

WPF WindowsFormsHost creates fuzzy text

I am hosting a winforms DataGridView inside a WindowsFormsHost on a WPF Window. Functionality is fine, but the text in the grid cells looks a little more fuzzy. Normally WinForms text has very little antialiasing.
How can I get the DataGridView text to look as it normally would on a Windows Form, or at least be sharper? I have tried playing with the TextOptions.TextFormattingModeand SnapsToDevicePixelssettings of the WindowsFormsHost, but don't see any difference.
As an example of what I mean here are two screenshots:
Datagridview inside a WPF WindowsFormsHost:
DataGridView in Windows Forms:
The appearance of hosted WinForms/Win32 content should not be affected by WPF. There is an "airspace" limitation that dictates that a single window pixel can only be owned by a single graphics API, and it can only be drawn by that API. Hence, WinForms content is drawn by WinForms/GDI+, even when hosted by WPF. This explains why hosted content will be drawn on top of any WPF content in the same window, even if the WPF content is positioned in front of it.
Changing WPF rendering properties like TextFormattingMode cannot affect the appearance of interop content. You should be looking at the layout/rendering properties of the hosted WinForms content.
Update
Presumably the fuzzy effect is do to WPF defaulting to grayscale AA for the DataGridView. In the Paint and/or CellPainting events of the DataGridView, setting the graphics text hint to cleartype:
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
eliminated most of the fuzziness.

Winforms toolbox tools for WPF

I'm new to WPF and I'm wondering is there anyway to have elements from Winforms toolbox in WPF. I mean the Winforms toolbox has a lot of elements and they're not present in WPF toolbox. for example something like PerformanceCounter in Winforms toolbox is not found in WPF toolbox.
thanks in advance
There are some controls that didn't get represented in WPF, but you can use WindowsFormsHost. As the name indicates, it's purpose is to host the Windows Forms elements.
Walkthrough: Hosting a Windows Forms Control in WPF
I would add that things like PerformanceCounter you can use in code, rather than placing it on a form. Other elements which do have a UI purpose can be placed inside a WindowsFormsHost control. I do this with ReportViewer -- it's a Windows Forms control that has no WPF equivalent.

Is it possible to have a project containing both Winforms and WPF?

Is it possible to have a project containing both Winforms and WPF?
Say a WinForm project that is transformed step by step(form by form) in a WPF one, will be possible to have a Winform opening on a button, and a WPF one opening on a other button?
Yes. You have to pick one technology to display each physical window and control in your app, but there's no reason why you can't mix and match.
For example:
A WinForms window can show a WPF window.
A WPF window can show a WinForms window.
A WinForms window can contain WPF content (see the ElementHost control).
A WPF window can contain WinForms controls (see the WindowsFormsHost control).
This works great.
One can have WPF windows in Windows Forms and Windows Forms windows in WPF
http://msdn.microsoft.com/en-us/library/ms745781.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx
Adding Winforms to WPF projects can be done smoothly (directly from the "Add new item" menu), but there is not straight option to add a WPF window to a Winforms project. Still, I handled to do it following these steps:
Add a WPF User Control (this option is available on the "Add new
item" menu) and then convert it into a WPF Window. Modify the XAML
changing the UserControl parent tag to Window, and remove the
inheritance from UserControl (all of this is explained in this link).
Add a reference to System.Xaml.dll. See this link.
Add a reference to System.Windows.dll (I found it on my computer on this path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5. Be aware it might be different in yours). See this link.
What you might be looking for is the ElementHost control. What it lets you do is take WPF content and host it in a Windows Forms window. More details are here:
http://msdn.microsoft.com/en-us/library/ms745781.aspx
There is also a control that lets you do the reverse: host Windows Forms content from within WPF:
http://nayyeri.net/host-windows-forms-controls-in-wpf
Between the two, you can move the 'dividing line' between WPF and Windows Forms with some degree of flexibility.
There is at one caveat you'll need to keep in mind. Windows Forms works internally in terms of HWND's... a window managed by the legacy Windows window manager (which handles the z-order). WPF doesn't do this... A WPF tree is typically rendered into a single HWND', and it's WPF that manages things like z-order. What this means to you is that z-order doesn't always work the way you expect it to, and there are things you can't do with hosted Windows Forms controls that you can do with traditional WPF elements. (There is actually a way to solve this, but it involves periodically rendering the HWND into a memory bitmap, rendering that bitmap into a WPF surface, and then redirecting events directed to the WPF surface to the underlying HWND. This is powerful, but tricky and difficult to get right.)
I see no objection to do that.(I have in WinForms Application WPF windows)
Many of the examples used MessageBox.Show which is part of the Windows.Forms.
Of course you must rewrite all windows, not only controls.

DataGrid for WPF and Silverlight

Is there a DataGrid component that behaves the same in WPF and Silverlight? There are some small differences in DataGrids from MS (WPF and Silverlight Toolkits). For example, while WPF version has CanUserAddRows property, Silverlight version does not.
I gave up - there nothing like that so I have to care about differences in the code by myself.
Edit: Most of the differences can be handled by styles - simply create one DataGrid style for WPF and one for Silverlight.

changing Visibility to hosted winForms element in wpf application

I have ReportViewer winForms control for showing Microsoft Reports in my WPF application hosted over WindowsFormsIntegration.
When the MouseDown event of my textBlock triggers, I would like it to fade lets say from 0 to 100 visability, like some other elements in my Window. It doesn't, so I was wondering is it the same for all hosted controls or this one specific and how can it be solved?
Regards,
Ivan
The WindowsFormsHost supports Transparency. But some WinForms Controls do not.
See the Supported Scenarios in Windows Presentation Foundation and Windows Forms Interoperation article on MSDN. Especially the first row in the "Behaviour" table:
Some Windows Forms controls do not
support transparency. For example, the
TextBox and ComboBox controls will not
be transparent when hosted by WPF.

Resources