In some scenarios, developers may find themselves in a situation where they are using the Infragistics WinGrid wrapped with some pretty intensive business logic that is going to take a good deal of time to recreate. One possibility that the developer can use is to continue forward with a WPF application and simply re-use the WinGrid in that application. This article demonstrates the necessary libraries to do that.
The first thing that needs to be done is to add references to the WindowsFormsIntegration dll and if you have a separate project hosting your WinForms control include that as well. With the addition of this WindowsFormsIntegration dll, you will be able to add a WindowsFormsHost component to your WPF application. To Display your Windows Forms control simply set the Child property and ensure you have a reference to the control. The following code snippet shows a WinGrid and a Carousel on the same Window.
<Window x:Class="XamWinFormsInteropSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="517" Width="680" xmlns:igDP="http://infragistics.com/DataPresenter" xmlns:WinGrid="clr-namespace:xamWinGrid;assembly=xamWinGrid" Loaded="Window_Loaded"> <Grid x:Name="MainGrid"> <Grid.RowDefinitions> <RowDefinition Height="162*" /> <RowDefinition Height="100*" /> </Grid.RowDefinitions> <WindowsFormsHost Name="windowsFormsHost1"> <WinGrid:xamWinGrid x:Name="xamWinGrid1" /> </WindowsFormsHost> <igDP:XamDataCarousel Name="xamDataPresenter1" Grid.Row="1" /> </Grid> </Window>
One other thing that can be done is in the user-control for the WindowsForms control; you can have a public property that exposes anything on the underlying controls that you may want to have access to in the WPF application. In this example, I will expose the DataSource property so that I can bind to a NorthWind dataset:
//Windows Forms User-Control public System.Collections.IEnumerable DataSource { set { this.ultraGrid1.DataSource = value; } }
To set up binding in a hybrid scenario, you can use the System.Windows.Forms.BindingSource control. An example from Microsoft is well documented here: http://msdn.microsoft.com/en-us/library/ms742687.aspx . That sample is the basis for the binding listed in this sample:
//WPF Application private System.Windows.Forms.BindingSource nwBindingSource; private NwindDataSet nwDataSet; private NwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter = new XamWinFormsInteropSample.NwindDataSetTableAdapters.CustomersTableAdapter(); public Window1() { InitializeComponent(); this.nwDataSet = new NwindDataSet(); this.nwDataSet.DataSetName = "nwDataSet"; this.nwBindingSource = new System.Windows.Forms.BindingSource(); this.nwBindingSource.DataSource = this.nwDataSet; } private void Window_Loaded(object sender, RoutedEventArgs e) { this.customersTableAdapter.ClearBeforeFill = true; this.customersTableAdapter.Fill(this.nwDataSet.Customers); this.MainGrid.DataContext = this.nwBindingSource; this.xamDataPresenter1.DataSource = this.nwBindingSource; this.xamWinGrid1.DataSource = this.nwBindingSource; BindingListCollectionView cv = CollectionViewSource.GetDefaultView(this.nwBindingSource) as BindingListCollectionView; cv.CurrentChanged += new EventHandler(cv_CurrentChanged); } void cv_CurrentChanged(object sender, EventArgs e) { BindingListCollectionView cv = sender as BindingListCollectionView; this.nwBindingSource.Position = cv.CurrentPosition; }
After doing this, you have officially included a Windows Forms Control in your WPF Application:
For additional information on this subject check out these links: