Window vs User Control - wpf

Is there a difference between window and user control? It seems to me that these two are exactly the same. So which one should I use and when?
(I tried googling this phrase and I couldn't find anything)

A Window is as the name suggests a window, it can be closed, minimized, resized etc. This should be quite intuitive.
A UserControl on the other hand is a composite component/module which can be placed inside other controls and is itself made up of controls (possibly even other UserControls), the main use for UserControls is reusability, encapsulation and loose coupling, some applications can be broken up into a set of UserControls of which each one provides a certain functionality.[citation needed]

We make user control if we want to reuse it. As name says User Control it means some control like grid,combo box like that.If i need same grid on 3-4 windows then i will prefer to make it as User Control.If it is not reusable i will define my grid in the required window.At last you paste your user control on some window.
Conclusion :- If you want to reuse the control then make it as a user control otherwise define it in required window.

A window is managed by the OS and is placed on the desktop.
A UserControl is managed by wpf and is placed in a Window or in another UserControl.
Applcations could be created by have a single Window and displaying lots of UserControls in that Window.

wpf window is a Win32 window, but user control is just something of wpf, not a Win32 window.

I presume you refer to the windows forms. Usually they are classified as user controls and custom controls - same stands for web forms as well. For more information you can refer to these links control vs user control in winforms and over view of user controls and custom contorls.

Related

Are native Windows Controls still used in .NET GUIs?

Is the stuff that is displayed when you create a GUI with WinForms or WPF still based on the native controls like Common Controls or any of the system provided Window Classes or is everything that is displayed "custom" drawn by the framework?
Note: I'm not talking about stuff like a file dialog, but GUI that was actually implemented with WinForms or WPF.
And yes, this is purely out of interest.
User HighCore commented:
WPF Uses an "HWND" for the Window objects, but then all other objects
(Controls) inside the Window are WPF objects not related to Win32 in
any way, wheareas AFAIK, winforms uses a separate HWND for each UI
element.
And indeed, using Spy++, we can observe the following when we create a test app with a simple dialog/window and a button on it:
MFC/native:
The app window is a Window (HWND) with the Window Class of #32770 (Dialog) (I used a "Dialog based" app.)
The Button is a separate Window (Class: Button)
Windows Forms:
The app window has the Window Class WindowsForms10.Window.8.app.0.2bf8098_r20_ad1 (oh my)
The Button is a separate Window (Class: WindowsForms10.BUTTON.app.0.2bf8098_r20_ad1)
WPF
There's only one top level Window, although for good measure I added a ComboBox, a ListBox, and a Menu to this window in the UI designer.
The Class of the only Window is: HwndWrapper[WpfApplication1.exe;;9b1aec0f-1b88-419c-8730-858906314cd9]
The Window Class names are actually quite interesting: With the MFC/native one you get the classes known for years and documented on MSDN. With Windows Forms, it does appear that it always uses the same class names. And with WPF it seems the name of a Class of a Window also incorporates the executable/process name.
So apparently MS thinks that using more than one Window per window isn't necessary anymore. I think I need to open a second question for that.
Windows Forms uses native controls for some UI elements. WPF draws everything on its own.

New window in Silverlight out-of-browser application

In a Silverlight application that doesn't run in a browser, is it possible to create a new top-level window? Or at least a child window?
I found some solution using the ChildWindow class, but even though my project is configured for Silverlight 4, that class can not be found.
I have a UserControl (XAML file) that I want to show as a new window. Using a tab control is not really an option unfortunately as the user has to be able to arrange windows to see more than one at once.
Any suggestions?
ChildWindow is part of the SDK, you need to add the System.Windows.Controls.dll to access the ChildWindow type.
I'm not sure you can get the ChildWindow to do what you are expecting. A ChildWindow is designed to present a window in a modal manner. However this modal behaviour is really a function of the ChildWindow template. It is possible to re-template to remove the modal behaviour. However I've never tried to manipulate multiple child windows. You could give it a go, the big question would be what happens if you close Child windows in a different order in which they were created?
In order to use a ChildWindow to present your UserControl it would probably be best for you to derive from ChildWindow, instead of UserControl. You may even find it would ultimately be better for you create base class between your specific Xaml and ChildWindow where you would put code that is common to all your windows.
I might be worth you noting the Silverlight 5 will support multiple windows.

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.

How to add a WPF window to a WinForms App

I'm creating a HUD window for inspecting biz entities in my WinForms application.
I wanted to have a completely different style of window (minimize the content area and showing only the TitleBar, no system buttons, etc) so I created a WPF application for this.
The problem is that I want this Window to 'live' inside my WinForms application. I can't just add the WPF as an OwnedForm or set the main Form as the Owner of the WPF window.
So, how can achive this?
EDIT: Thanks to pst I found the answer. Here is the snippet:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(_inspector);
WindowInteropHelper inspectorHelper = new WindowInteropHelper(_inspector);
inspectorHelper.Owner = this.Handle;
_inspector.Show();
A WPF Window has a Win32-window handle/context.
See WindowInteropHelper. You can use this with Win32 (or perhaps there is WinForms support?) to set the owner window of the WPF Window. Be aware the handle does not exist until the "source initialized" (?) event.
However, using just WinForms, you may be able to customize the titlebar as much as you need (you can overwrite the drawing itself via Win32, and I think you lose all the control boxes without going this far).
There are lots of google results on this topic if you use the correct keywords.

Have a wpf window inside another wpf window

Is there a way to host a WPF window inside another WPF window. I have a couple of somewhat complex forms in place. But now to simplify things I am trying to merge a few of them as tabpages inside one 'Dashboard' form.
Please note that I am not trying to host a Windows Form, but another WPF window
If you want tabpages, why not use a TabControl with UserControls inside ? If you need to transform one of these tabs to a floating window, just put the UserControl in a new Window...
Can I answer this question with another question; why would you not create them as controls rather than other WPF windows, that you want to host in the main WPF window?
a bit late on this, but I guess with WindowsForms interop you can put in WPF a WinForms control host and in that host put a WinForms control that hosts the handle of a WPF window
I think what you're asking for is MDI, Multiple Document Interface. Something like this might help.
Do note, however, that the MDI paradigm is largely shunned these days. There are usually better ways to achieve the same functionality.
I think you want to hosting contents of WPF Window1.xaml (page1.xaml) inside within another WPF Window.
Well...you can use Navigation. Instead running window1.xaml contents inside tab then you can work with your data inside Navigation. Navigation can run within WPF Window Application. You just design your form / UI in page1.xaml.
one another..MDI old and rusty. We want clear of top window nowadays.

Resources