是否无法绑定到属于C#/XAML应用程序中WindowsFormsHost子对象的属性的解决方法? [英] Workaround for inability to bind to a property that belongs to a WindowsFormsHost Child object in C#/XAML app?

查看:302
本文介绍了是否无法绑定到属于C#/XAML应用程序中WindowsFormsHost子对象的属性的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#WPF 4.51应用程序.据我所知,您不能绑定到属于WPF WindowsFormsHost 控件的子级对象的属性. (如果我在这个假设中错了,请告诉我该怎么做):

I have a C# WPF 4.51 app. As far as I can tell, you can not bind to a property belonging to an object that is the child of a WPF WindowsFormsHost control. (If I am wrong in this assumption please show me how to do it):

与WindowsFormsHost绑定

就我而言,我有一个页面,其中包含一个 WindowsFormsHost 控件,该控件的 Child 对象是一个 ScintillaNET 编辑器控件:

In my case, I have a page that contains a WindowsFormsHost control whose Child object is a ScintillaNET editor control:

https://github.com/jacobslusser/ScintillaNET

    <WindowsFormsHost x:Name="wfhScintillaTest"
                      Width="625"
                      Height="489"
                      Margin="206,98,0,0"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Top">
        <WindowsFormsHost.Child>
            <sci:Scintilla x:Name="scintillaCtl" />
        </WindowsFormsHost.Child>
    </WindowsFormsHost>

子控件正常工作并显示正常.如果它是正常的WPF控件,则可以将 Scintilla 编辑器控件的 Text 属性绑定到我的 ViewModel ,因此更新 Scintilla 编辑器控件内容所需要做的就是更新 string 属性.

The child control works and displays fine. If it were a normal WPF control I would bind the Text property of the Scintilla editor control to some string property in my ViewModel, so that all I had to do to update the content of the Scintilla editor control is update that string property.

但是,由于我无法绑定到属于 WindowsFormsHost 子对象的属性,因此我正在寻找一种不会完全笨拙或笨拙的策略/解决方案.有没有人以前遇到过这种情况,并且有解决我的绑定/更新问题的合理策略?

But since I can't bind to a property belonging to a WindowsFormsHost child object, I'm looking for a strategy/solution that not completely awkward or kludgy. Has anybody faced this scenario before and has a reasonable strategy that solves my binding/update problem?

推荐答案

这里的一种简单方法是,您可以创建一些专用类,以仅包含附加属性,这些属性映射到Winforms控件中的属性.在这种情况下,我只选择Text作为示例.使用这种方法,您仍然可以正常设置Binding,但是附加的属性将在WindowsFormsHost上使用:

A simple approach here is you can create some dedicated class to contain just attached properties mapping to the properties from your winforms control. In this case I just choose Text as the example. With this approach, you can still set Binding normally but the attached properties will be used on the WindowsFormsHost:

public static class WindowsFormsHostMap
{
    public static readonly DependencyProperty TextProperty
        = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(WindowsFormsHostMap), new PropertyMetadata(propertyChanged));
    public static string GetText(WindowsFormsHost o)
    {
        return (string)o.GetValue(TextProperty);
    }
    public static void SetText(WindowsFormsHost o, string value)
    {
        o.SetValue(TextProperty, value);
    }
    static void propertyChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var t = (sender as WindowsFormsHost).Child as Scintilla;
        if(t != null) t.Text = Convert.ToString(e.NewValue);
    }
}

在XAML中的用法 :

Usage in XAML:

<WindowsFormsHost x:Name="wfhScintillaTest"
                  Width="625"
                  Height="489"
                  Margin="206,98,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  local:WindowsFormsHostMap.Text="{Binding yourTextProp}"
    >
    <WindowsFormsHost.Child>
        <sci:Scintilla x:Name="scintillaCtl"/>
    </WindowsFormsHost.Child>
</WindowsFormsHost>

Child当然应该是Scintilla,否则您需要修改WindowsFormsHostMap的代码.无论如何,这只是为了展示想法,您可以随时对其进行调整以使其变得更好.

The Child should of course be a Scintilla, otherwise you need to modify the code for the WindowsFormsHostMap. Anyway this is just to show the idea, you can always tweak it to make it better.

请注意,上面的代码仅适用于1种方式的绑定(从视图模型到winforms控件).如果您希望使用其他方法,则需要为控件注册一些事件处理程序,并将该值更新回该处理程序中的附加属性.这样很复杂.

Note the code above works just for 1 way binding (from view-model to your winforms control). If you want the other way, you need to register some event handler for the control and update the value back to the attached property in that handler. It's quite complicated that way.

这篇关于是否无法绑定到属于C#/XAML应用程序中WindowsFormsHost子对象的属性的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆