Windows Forms User Controls in WPF app - wpf

Could someone post a link to simple tutorial, which describe how to use User Controls, created in Win Forms in WPF aplication's?
Thank's a lot.

In a nutshell:
1.Add a reference in your WPF project
to System.Windows.Forms and
WindowsFormsIntegration
2.Add the namespace in your XAML:
xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
3.Use the WindowsFormsHost tag to surround any WinForms controls you use:
<Grid>
<WindowsFormsHost>
<WinForms:DataGrid />
</WindowsFormsHost>
</Grid>
4.Profit!

Related

Is it possible to include custom C++\WinRT UWP controls in WPF with XAML Islands and WindowsXamlHost?

I have a WPF application that is too onerous to rewrite wholesale in UWP. Some of the UWP controls would utilize SwapChainPanel and thus have C++/WinRT to manage DirectX. To determine the feasibility of implementing portions of the application in UWP and including them in WPF, I made a minimal sample app following Microsoft documentation that attempts to compose UWP controls in a WPF application targeting .NET Core 3.1.
<Window x:Class="WpfAppCore3._1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xh="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost"
Title="WPF App"
Height="500"
Width="800">
<StackPanel>
<xh:WindowsXamlHost InitialTypeName="UwpLib.ManagedGrid"
Height="225" />
<xh:WindowsXamlHost InitialTypeName="UwpLibNative.NativeGrid"
Height="225" />
</StackPanel>
</Window>
This works great for a managed control like UwpLib.ManagedGrid but UwpLibNative.NativeGrid does not load:
The debugger shows an exception:
System.BadImageFormatException: 'Bad IL format.'
That exception indicated to me a build configuration issue, but I think the application is set up correctly in that regard. Is this just not possible with XAML Islands today or have I made some configuration mistake in the sample app?
Update 1:
I discovered the "Windows Desktop Compatible" option and made sure that was set to "Yes". No change.

Adding a new IronPython WPF Window to complains "Window is not supported in a WPF project"

Immediately after adding a brand new, untouched WPF window to my IronPython 2.7 project in VS2013 (with Python Tools for VS 2.0.11016), it tells me "Invalid Markup" in the design window, and the error list shows:
Window is not supported in a Windows Presentation Foundation (WPF) project.
Grid is not supported in a Windows Presentation Foundation (WPF) project.
The XAML window has this innocuous looking code in it:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
</Grid>
</Window>
Does Python tools for VS not really support form creation? Did I forget to configure something?
The project was started as an "IronPython Windows Forms Application" rather than an "IronPython WPF Application" so it was missing the relevant references:
PresentationCore
PresentationFramework
WindowsBase
Adding them makes WPF forms functional, or just recreating the project.
If all of those references appear to be in the project, removing/readding some might help. Another user reported he had to do so for 'PresentationFramework'; perhaps there's a couple that have the same name?

Using control templates in user controls...?

I'm really new to WPF and I'm trying to change the hover colour of a button in WPF. I've figured out I need to create a control template in order to efficiently do this, which I've been able to successfully do in a standard WPF application which has a App.XAML file, however the application that I'm using isn't a full WPF app, it's a winforms app that uses a ElementHost to link a WPF user control into the form. Soooooooo I was wondering how do I create a control template for WPF user control? I don't have a app.XAML which is where I put the control template the first time I did it, and if I try to slide the control template into any of the user control XAML it throws an error.
Thanks
When you're creating the template in App.xaml you're adding it as a resource by putting it inside the Application.Resources resource dictionary. You can do the exact same thing on any other element in XAML that represents a FrameworkElement (i.e. any control, layout panels, etc.). The basic setup is like this:
<UserControl ...>
<UserControl.Resources>
<ControlTemplate x:Key="MyCustomTemplate" .../>
</UserControl.Resources>
</UserControl>

How do I create UserControl in IronPython WPF project?

Iron Python Tools Visual Studio 2010 does not provide a way to add WPF User Control in a IronPython WPF project? How do I make one? I tried copy-pasting User Control from C# WPF project but it did not work. Please help.
It might not be quite the same as what you're asking, but I wanted to make a UserControl the content of a TabItem, to keep the XAML for the tabs in separate files.
I haven't found a clean way in IronPython, but I have found a reasonable hack. I imagine it would work for components other than TabItems, too.
First, I named my TabItem, like this:
<TabItem Name="my_tab_item" Header="My Tab Item" />
Then I loaded my UserControl's XAML into a custom class:
import wpf
import clr
clr.AddReference("System.Xml")
from System.Windows.Controls import UserControl
class MyUserControl(UserControl):
def __init__(self):
wpf.LoadComponent(self, 'my_user_control.xaml')
And I added it to the TabItem like this:
self.FindName("my_tab_item").Content = MyUserControl()

is it possible to use Winform components in WPF

We have a product which is developed using Winforms - .net 2.0. Now we are thinking about migrating this application to WPF. Is is possible to use the same Winform components in WPF.
Or if it is not possible, then which is best possible ways to Migrate this application to WPF.
Yes. You can use ElementHost to put WPF content into Windows Forms controls, and WindowsFormsHost to host your Windows Forms component directly within a WPF element.
There's a tutorial on Switch On The Code using the DataGridView.
You need to add references to System.Windows.Forms and WindowsFormsIntegration to get WindowsFormsHost.
You can do it in the XAML as well as the C# code:
<WindowsFormsHost Grid.Row="0">
<WinForms:DataGridView x:Name="_MyDataGrid">
</WinForms:DataGridView>
</WindowsFormsHost>
C#
_MyHost = new WindowsFormsHost();
_MyDataGrid = new System.Windows.Forms.DataGridView();
....
_MyHost.Child = _MyDataGrid;
_Container.Children.Add(_MyHost);

Resources