Is it possible to show xaml view inside windows form - c#

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

Related

WPF Form loses it style after showing from another project

I have a C# application which contains 2 project, and when I set the first project which contains a Styled WPF Form as startup project, it has it's own style and everything is fine. But when I use this code to show that WPF from from a Windows Form Application, the first project's form loses its own style:
Window introForm;
introForm = new Client.MainWindow();
introForm.Show();
I have no idea why it happens
I have found my answer here:
https://stackoverflow.com/a/6042515/7147513
this fixed my problem:
var foo = new Uri("pack://application:,,,/MyProject;component/Resources/Styles.xaml", UriKind.RelativeOrAbsolute);
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });

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

Adding a form into TabControlPage in C# Windows Forms Application

I have two forms that I am trying to display on a master form using a tabControl element in C#.
I have had no luck so far. How does one go about it?
In your "Child" form, put all your control in a Panel (let's call it "movingPanel") with its Dock property set to Fill and its Modifiers property set to internal. After creating, the child form in the master form, simply do:
theChildForm.movingPanel.Parent = tabControl1.TabPages["The_tabPage_Name"] ;
If you want to host your form into a tab page I think this part of code helps :
newForm.TopLevel = false;
newForm.ControlBox = false;
newForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
newForm.Dock = DockStyle.Fill;
var newTab = new TabPage()
newTab.Controls.Add(newForm);
this.MainTabControl.TabPages.Add(newTab);
this.MainTabControl.SelectedTab = newTab;
newForm.Show();
I hope this is what you are looking for.

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?

Public class modifier for WPF control

I'm creating Windows application and Class library. Class library contains WPF control named "InsertForm.xaml"
InsertForm contains TextBox named eUserName.
I'm using the following code to show InsertForm. That's successful. But I can't access eUserName. How to set Textbox modifiers to public?
using System.Windows.Forms.Integration
ElementHost host = new ElementHost();
iform= new Extender.InsertForm();
host.Child = iform;
this.Controls.Add(host);
Would this work?
<TextBox Name="eUserName" x:FieldModifier="public"/>

Categories

Resources