开发者

How do I databind to a Winform control that is hosted in a WPF control?

I just thought abou开发者_JAVA技巧t converting one of our custom controls to WPF, however it uses another custom control of ours that is written Winforms.

As there is little point in using WPF without MVVM. How can I databind to a Winforms control that is used within a WPF control.

(I don’t have much WPF experience, so I may be missing the point completely)


I think you will have to wrap the WinForms control in a class exposing DependencyProperties or at least implementing INotifyPropertyChanged.

So you'd have a class such as:

public class WinFormsWrapper : WindowsFormsHost
{
   //You'll have to setup the control as needed
   private static MyWinFormsControl _control;

   public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register("IsSpinning", typeof(bool), typeof(WinFormsWrapper), 
        new FrameworkPropertyMetadata(_control.IsSpinning, new PropertyChangedCallback(IsSpinning_Changed)));

    private static void IsSpinning_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        _control.IsSpinning = (bool)e.NewValue;
    }

    public bool IsSpinning
    {
        get { return (bool)GetValue(IsSpinningProperty); }
        set { SetValue(IsSpinningProperty, value); }
    }
}

Assuming that you have an IsSpinning property on your WinForms control. Depending on your needs, it may be simpler to implement INotifyPropertyChanged instead of using DependencyProperties.


Added by Ian Ringrose:

This example code is clearly wrong (see comments about _control being static), but shows how to solve the problem. As I am not using WPF at present, I am not going to edit the code as I can't test my edits.

I am keeping this answer as the accepted answer, as it contains the information needed to solve the problem.

I have added this to the answer, as comments do sometimes get deleted and this is getter views from people doing google searches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜