How to bind a windows control in WPF? - wpf

I'm using WPF 4.0. I have a WPF Datagrid. One of my column in a datagrid is a template column. In that template column I have used Win forms Textbox and my problem is that,
How to bind that win forms texbox control in WPF?
How to access that control or column in Code behind (c#)?

You can't use WPF data binding with WinForms controls, WPF data binding requires dependency properties and FrameworkElement derived objects - both are part of WPF and not available in WinForms.
WinForms has it's own data binding system, it's completely incompatible with WPF's data binding (it's also weaker and unusable in some cases).
If you want to use WPF's databinding you have to use only WPF controls.
I suggest you either use the WPF TextBox or switch the entire grid to WinForms and use DataGridView.
by the way - I wouldn't put a WinForms control inside a WPF data grid (or any other items control) - I suspect this will give you a lot of trouble in the future.

Related

Enable datagridview in vs2012?

How do I enable DataGridView?
Currently it is greyed out in the toolbox, see this screenshot.
DataGridView is a Windows Forms control, as your screenshot shows. Putting WinForms in WPF is usually avoided where possible as they are different technologies - you should aim to use a WPF equivalent.
If you really want to use it, you can use a WindowsFormsHost - there are lots of guides out there to explain how.
In fact, DataGrid is the WPF equivalent of DataGridView, e.g. WPF DataGrid Vs Windows Forms DataGridView. I'd use DataGrid if I were using WPF.

Binding a WPF element host to a WinForm property

I have a WPF element host sitting in a WinForm.
I would like to bind a property of the element to a property of the WinForm.
In my searches I found the opposite solution, binding between WinForms to WPF properties (when the WinForm is sitting on a WPF host).
Here is an example:
How to bind a windows control in WPF????
I also saw this answer (not exactly an answer, I'd say a conclusion): Binding to a WPF hosted control's DependencyProperty in WinForms
It did not help me. I also want to keep the the principle of MVVM in the WPF element host.
|It there a way to do this?
Finally I solved it this way:
I created a property in ViewModel and init him through the WinForm when I use the wpf host control in the first time.
I binding the property to element in xaml
and that's all.

How to implement two way binding between an ActiveX control and a WPF MVVM View Model

I have a WPF application implemented using the MVVM framework that uses an ActiveX control and I need to keep the WPF and ActiveX UI synchronised.
So far I can update the ActiveX UI when I change the WPF UI using the code at the bottom of the question that I got from the article Hosting an ActiveX Control in WPF and this question.
But I cannot update the WPF UI when I make a change in the ActiveX UI.
I suspect that I need to fire the PropertyChanged event from my ActiveX control but I have no idea how to do this or if it is even possible.
The ActiveX controls I have written are in VB6 and MFC as I am just prototying at this time for the eventual integration of VB6 ActiveX controls in a WPF contaner application.
Here is a code snipet that indicates the work done so far:
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the ActiveX control.
AxTEXTBOXActiveXLib.AxTEXTBOXActiveX axWmp = new AxTEXTBOXActiveXLib.AxTEXTBOXActiveX();
// Assign the ActiveX control as the host control's child.
host.Child = axWmp;
axWmp.DataBindings.Add(new System.Windows.Forms.Binding("ActiveXStatus", (MainWindowViewModel)this.DataContext, "ModelStatus", true, DataSourceUpdateMode.OnPropertyChanged ));
// Add the interop host control to the Grid
// control's collection of child controls.
this.activexRow.Children.Add(host);
How to implement two way binding between an ActiveX control and a WPF MVVM View Model?
What you need to do is create an ActiveX library that both the ActiveX control can refer too and the WPF assembly. In the library create an interface with a refresh method of whatever complexity you need. Create a global multi-use method that objects that implement can use to register themselves. Then in the ActiveX control it can check to see whether anything been registered and fire the refresh method. Then in the WPF Assembly the UI can implement the interface and register itself supplying the connection between it and the ActiveX Control.
You're going to have to find some way of notifying WPF that data in the ActiveX control has changed. This is usually handled in WPF by implementing INotifyPropertyChanged or using ObservableCollections. Just binding a property doesn't update the UI for WPF either, try binding something in your view to a property on the viewmodel and not using INotifyPropertyChanged. If you have control of the activex source this should be easy to do using events.

Where can i find Free WPF controls

Is there any site available where I can find free WPF controls like Griview ? I am currently working on a complex project where I need a customizable WPF Gridview like telerik
You can use controls from the WPF Toolkit as well as Extended WPF Toolkit.
Have a look at the controls in AvalonControlsLibrary on code plex, it is having a DataGridView control apart from other controls. -
http://avaloncontrolslib.codeplex.com/wikipage?title=Home&ProjectName=avaloncontrolslib
DataGridView
DataGridView is a maybe a misleading name for this control. This control is far from being the same as the WinForms DataGridView (maybe someday it will J). Basically this control is a WPF ListView control but it is capable of auto generate the GridViewColumns for you. It generates the columns by looking at the objects’ properties. You can also specify how you generate the columns by decorating your properties with a custom attribute. For more information have a look at this post. http://marlongrech.wordpress.com/2007/09/01/listview-with-auto-generation-of-column-enable-disable-columns/
Similar SF question - Where can I find free WPF controls and control templates?
On codeplex.com and codeproject.com. However the best always cost money.

DataGrid for WPF and Silverlight

Is there a DataGrid component that behaves the same in WPF and Silverlight? There are some small differences in DataGrids from MS (WPF and Silverlight Toolkits). For example, while WPF version has CanUserAddRows property, Silverlight version does not.
I gave up - there nothing like that so I have to care about differences in the code by myself.
Edit: Most of the differences can be handled by styles - simply create one DataGrid style for WPF and one for Silverlight.

Resources