Classic forms control cannot resize in WPF environment - c#

I've added a regular forms control but I cannot resize it. Instead I have to resize host.
MSDN sample: Hosting a Windows Forms Control in WPF
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
host.Child = mtbDate;
this.grid1.Children.Add(host);
mtbDate.Width = 200; //Not work!
host.Width = 200; //Workaraound...
How can I resize the control, not the host?

This page gives a lot of information about hosting WinForms controls in WPF: http://msdn.microsoft.com/en-us/library/ms744952.aspx
The short story is that you are not supposed to resize the textbox- that will be ignored/overridden. Instead, resize the WindowsFormsHost (either in WPF or via WPF dynamic layout)

Related

WPF window inside a WinformHost Panel

I am showing a WPF exe window inside another WPF application using winform host.
I have created a panel in main application and set it as child of winformhost.
mHostingPanel = new System.Windows.Forms.Panel()
{
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
};
mWinformHost = new WindowsFormsHost();
mWinformHost.Child = mHostingPanel;
and then I start the other window process and set hosting panel as parent.
WindowsAPI.SetParent(mProcess.MainWindowHandle, mHostingPanel.Handle);
My question is if I launch the application,Who will be rendering my Child WPF window whose parent is a winform panel.Will it be Direct-X or GDI context of Panel?
Also if I set Allowtransparency=True on child WPF application,The UI doesnt show up in hosting panel.
Found the reason.Its called airspace issue (when win32 and WPF trying to share pixels) and I dont think it can be solved by any framework API as microsoft denied it.
https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2644120-bring-back-the-hwndhost-isredirected-and-compositi
Mitigating AirSpace issues

equivalent WindowsFormsHost in C# winform

what is equivalent WindowsFormsHost in C# winform?
i wrote program in C# winform. in my user control(instead of form), i put reporting. when user clicked btnReporting, rptfile is shown.
this is my code in wpf. i don't equivalent WindowsFormsHost in C# winform?
CrystalReportViewer rptViewer = new CrystalReportViewer();
rptViewer.DisplayStatusBar = false;
WindowsFormsHost host = new WindowsFormsHost();
rptViewer.ReportSource = rep;
host.Child = rptViewer;
ReportRpt PageRpt = new ReportRpt();
PageRpt.GrdReport.Children.Add(host);
NavigationService navService = NavigationService.GetNavigationService(this);
navService.Navigate(PageRpt);
For showing crystal report on Winform, you may add the Crystal Viewer from the toolbox on the form and set the crystal report against it.
Please see: Displaying Crystal Reports using WinForms and C#
but if you want to host a WPF control in Winform then you can use ElementHost
A Windows Forms control that can be used to host a Windows
Presentation Foundation (WPF) element.
you dont need a windows forms host in winforms - thats just for backwards compatibility in WPF
just add the reference to crytal reports and place the control on your form

Rendering issue with WPF controls inside ElementHost

I am having a WinForms control, inside that I have a TableLayoutPanel which holds multiple ElementHosts and each ElementHost contains a WPF control.
Everything works fine except when the size of controls is bigger then window and ScrollBar is there; when I scroll down, the controls get rendered distorted, like this -
On maximizing the window or re-sizing it, controls render properly
(reducing the size such that controls go out of visible area and then increase the size again to bring them back in visible area)
This doesn't happen with WinForms control in the same window just the WPF ones; any idea why this is happening and any solution for this?
this.Loaded += delegate
{
var source = PresentationSource.FromVisual(this);
var hwndTarget = source.CompositionTarget as HwndTarget;
if (hwndTarget != null)
{
hwndTarget.RenderMode = RenderMode.SoftwareOnly;
}
};
Try using that in the wpf control you are hosting. This is a known rendering issue of the the wpf controls that are hosted in win forms. Changing the rendering mode to software only will solve the problem.
I had a similar problem and solved forcing a refresh of the ElmenetHost in the scroll event of the TableLayoutPanel
Ok, this is gonna sound like total B.S. but it worked for me: in the Load event of your form, resize the form.
public class MyForm : Form
{
public MyForm()
{
Load += (o, e) => { Width -=1; Width +=1; };
}
}
After the form has been resized, I could not force a display issue.

Is it possible to show xaml view inside windows form

I have a windows form which calls other windows form that work within my application. What I want to accomplish is to get away from this whole "windows form" thing and use WPF View (usercontrol) instead. Is there a way I can call a view to show it from my form?
ElementHost host = new ElementHost();
Cars.WPF.Views.DescriptionView descView = new Cars.WPF.Views.DescriptionView();
host.Controls.Add(descView);
host.Dock = DockStyle.Fill;
I get error: --> Argument 1: cannot convert from 'Car.WPF.Views.DescriptionView' to 'System.Windows.Forms.Control'
Add a panel in your winform (lets say panel1)
Define ElementHost at class level, Also define WPF Control at class level
ElementHost host;
Cars.WPF.Views.DescriptionView descView;
In form load event do:
host= new ElementHost();
panel1.Controls.Add(ctrlHost); //Add Element host to panel1
descView = new Cars.WPF.Views.DescriptionView();
descView.InitializeComponent();
host.Child = descView; //Instead of adding WPF control to Winform do this
Also in your project references add:
PresentationCore
PresentationFramework
WindowsBase
Yes, use Element Host.
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx

Casting Windows.Controls.UserControl to Windows.Forms.Control

Can we cast a WPF User Control to a form control??
I'm sorry you can't. WPF works very differently internally from Winforms: Winforms uses the controls provided by the Windows OS (where each control has a window handle), where WPF uses DirectX to do the painting.
You can host WPF controls inside winforms applications (EDIT)and vice versa (with limitations) but that is perhaps not what you're after.
I tried this out:
TouchScreenWPF touchUI = new TouchScreenWPF();
ElementHost elementHost = new ElementHost();
elementHost.Child = touchUI;
Control userControl = new Control();
userControl.Controls.Add(elementHost);
The form contains the usercontrol, but does not display anything when I include a WPF User control. It works with a single button though... Am I missing something there?

Categories

Resources