Inserting Winforms Form inside XAML WPF - wpf

I'm trying to integrate a WinForms Form inside a WPF XAML page.
I have seen examples of putting Windows Forms controls using WindowsFormsHost, but not for integrating a whole form.
Is there a way to do it? The Windows form is hosted inside the WPF application and I'm currently able to load it as a new window but not inside the XAML page.

To put a WinForms form inside a WindowsFormsHost tag in a WPF Form is an error. But you can instantiate a new form from any WPF Project. Here's how:
Say you have two projects "WinFormProj" and "WPFProj". WinFormProj has a form called "Login".
First add a reference in the WPFProj project to WinFormProj.
Then behind any event in any WPFProj element, do this:
var winFormWindow = new WinFormProj.Login();
winFormWindow.Show();
// or
windFormWindow.ShowDialog();
If that won't do, you can convert your WinForms form to a user control which will exist just great in a WindowsFormsHost WPF tag. To do that, it is often as simple as ...
Go to the class declaration and change:
public class Login : Form
to
public class Login : UserControl
Next, in your WPF form, add the WinForms project as an xml namespace:
xmlns:winForms="clr-namespace:WinFormsProj;assembly=WinFormsProj"
And finally, add this to your Grid/StackPanel/Whatever where you want the user control to be hosted:
<WindowsFormsHost Name="LoginControl">
<winForms:LogIn />
</WindowsFormsHost>
Pretty simple once you demystify it.

Related

WPF Displaying Windows

I have been looking for a good tutorial on WPF, MVVM and windows navigation. I am trying to display a new window once a user clicks an "ok" button. Does anyone know how to go about doing this?
To display a new window using an OK button, you need to create a new instance of the window and call it's Show() method. You can either do this in the button click event (code behind) or bind it to a custom Command object (MVVM). Here's the code to open a window.
var window = new MainWindow();
window.Show();
In MVVM, some developers choose to just have 1 Window, usually MainWindow, and separate parts of their UI into UserControls. They use DataTemplates to define which UserControl appears in the MainWindow.
There are a lot of tutorials on MVVM if you just take time to Google this topic. Here's a few links that have helped me a lot.
http://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Making a Wizard in Silverlight + Xaml

I have a Silverlight application and I am trying to make each step of a wizard in XAML files, instead of hard-coded C#.
The problem is that I don't understand how I am going to switch between them after click on next button of each screen.
What is the best way to do this? I saw some tutorials on the internet about XAML dynamically loaded but none of them seem to work with me :/
Use a ChildWindow as your parent window. Then create multiple UserControls which will be framed in the content of the parent window. In the code-behind of the parent window, load the user controls into a list and set the visibility to 'Collapsed' for all of them but the first. When the user presses the Next/Prev buttons, pull the appropriate UserControl from the list (keep track of the current index) and make it 'Visible' while making the current control 'Collapsed'.
All of your navigation code will be in the parent window, but the parent window won't be concerned about the content of the wizard steps itself.
Each UserControl can be coded in XAML as a separate control so you still maintain a separation of the control from your wizards navigation logic.
You can then create a class type that will hold all of the options for the various wizard controls. Pass a reference to an object instance to each of the controls for them to update. When you get to the end of the wizard, your option object should maintain the state of all the wizard steps and the parent window can return that to the application.
I would suggest looking into the Silverlight Navigation Framework. It allows you to use "urls" to navigate between "pages" (which are your XAML user controls). It also also users to use the back and forth buttons in the browser, which may or may not be something you want to allow.
There is a VS 2010 template when you choose New Project, Silverlight, "Silverlight Navigation Application" that will help get you started.

How to make a wpf user control containing several user controls

I have created a WPF User control library. It has by default 1 user control in it. I added two more user controls on it. Added next back button to everycontrol. Now I am stuck here. What should I do to navigate from one user control to another? Can I do that in MVVM pattern? if yes then how?
Try downloading Telerik trial pack, they have a demo app (with source) that allows to browse their control. An once you have that demo app you can switch the user control dll in background when an update occurs.

Silverlight App inside WPF WebBrowser?

I have a hopefully trivial question. Currently, my company works with a rather obscure language (SyngergyDE) and we need to call a SilverLight application inside our product. Unfortunately, this obscure 3rd party language only (currently) supports the opening of WPF screens. So with that said, I thought I'd develop a small WPF user control that contains a "WebBrowser" control and navigate to the silverlight application's URI. This works fine, and I'm able to see the SL application. Here is my question - we have a "Close" button on the SL application, and when users "Click" that button, we want the window to close.
Does anyone have any suggestions on how we can communicate the "Closing of the SL App" to the WPF user control, so that the entire WPF user control closes as well?
Thanks everyone,
-Tom
Attach an event handler to the WebBrowser.Navigated event.
Have the close button in the Silverlight application use:-
HtmlPage.Window.Navigate(new Uri("about:blank", UriKind.Absolute));
When the Navigated event fires in WPF with the url "about:blank" then its time to close the control.
Use Javascript and the HTML DOM as the glue here.
For example, when the SL app close button is clicked, have Silverlight trigger some Javascript code that sets a flag, or alternately, raises some HTML document event.
The WPF control could poll that flag in the HTML + Javascript, or alternately listen for that HTML document event, then close the user control.

How to prevent WebBrowser control from stealing focus from other text box in my WPF form

My WPF app has a window with various text boxes and a WebBrowser control.
The WebBrower control is used to show a web site (I doesn't own) and then a Login button in the WPF window fills the fields in the site from the text boxes.
The problem is that the web site calls focus() method on one of its input fields on load, and that still the focus from the text box in my WPF window. I want the focus to be on the WPF window field and not in the web page within the WebBrowser control.
I tryed calling MyTextBox.Focus() in the WebBrowser's LoadCompleted event, but to no avail. Setting the WebBrowser's IsEnabled or even Visibility properties didn't help either.
Does anyone have an idea how I can overcome this and move the focus to the WPF control instead of the web page input field?
Don't think you can extend the control site to add IProtectFocus to the site. Try interop the Windows Forms version of the webbrowser control and override the CreateWebBrowserSiteBase method to provide your own control site.

Resources