I create a simple WPF UserControl (any control will do) and insert it into a Winform (using an ElementHost).
This works fine. The WPF control is embedded in the WinForm and when the WinForm is moved around the WPF control moves with it just like a WinForm control will do.
When I change the WinForm to transparent by setting the TranparencyKey to the same value as the BackColor some problems occur:
- The WPF control is not displayed - instead a black rectangle is displayed in its place..
- If the WinForm is moved around the screen then on its original place the WPF control appears - as if it was covered by the WinForm.
Changing properties of the ElementHost (such as BackColorTransparent) did not change the behavior.
Setting both the BackColor and the TransparencyKey of the WinForm to Black got rid of the black rectangle problem but still when the WinForm was moved the WPF control was left in the original place.
Note also that these problems occurs under Windows XP SP3 (which is our target OS) but not on Windows 2003.
(We did not check on Vista and Windows 7).
Any ideas how to have a transparent Winform with a WPF control inside and still get a normal behavior?
Did you set the background property on the WPF control? By default it inherits from it's parent container and if it's not present it will do this. Try setting the background property on the UserControl to white or grey and your problem should go away.
Related
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?
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.
My crazy problem: I'm using a docking system (AvalonDock) on a WPF form with aero glass style background (what looks really nice). In one dockable MDI I need a WinForms custom control, so I just used the WindowsFormsHost that loads my control successfully. But there is one problem:
The WindowsFormsHost shows the glass background of the main form for the amount of black for all colors used in the custom control.
Changing the MDI background doesn't help because the WindowsFormsHost cuts a hole through everything in the WPF MDI.
Concluding neither trying to have white text on a black background nor the other way round makes the textbox in my custom control clearly readable.
Any suggestions?
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.
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.