c# - 在 C#/XAML 应用程序中无法绑定(bind)到属于 WindowsFormsHost 子对象的属性的解决方法?

标签 c# wpf xaml scintilla windowsformshost

我有一个 C# WPF 4.51 应用程序。据我所知,您不能绑定(bind)到属于 WPF WindowsFormsHost 控件的子对象的属性。 (如果我的假设有误,请告诉我如何去做):

Bind with WindowsFormsHost

在我的例子中,我有一个包含 WindowsFormsHost 控件的页面,其 Child 对象是一个 ScintillaNET 编辑器控件:

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 属性绑定(bind)到我的 ViewModel< 中的某个 string 属性/em>,因此我更新 Scintilla 编辑器控件的内容所需要做的就是更新 string 属性。

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

最佳答案

此处的一个简单方法是您可以创建一些专用类来仅包含映射到 winforms 控件中的属性的附加属性。在这种情况下,我只是选择 Text 作为示例。使用这种方法,您仍然可以正常设置绑定(bind),但附加属性将在 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 中的用法:

<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的代码。无论如何,这只是为了展示这个想法,您可以随时对其进行调整以使其变得更好。

请注意,上面的代码仅适用于一种方式绑定(bind)(从 View 模型到您的 winforms 控件)。如果您想要另一种方式,则需要为控件注册一些事件处理程序并将值更新回该处理程序中的附加属性。这样就相当复杂了。

关于c# - 在 C#/XAML 应用程序中无法绑定(bind)到属于 WindowsFormsHost 子对象的属性的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074176/

相关文章:

c# - xaml 按钮模板和按钮上方的按钮

c# - 用于缩小从 WebMethods 返回的 ASP.NET 3.5 XML 文档的选项

c# - 将凭据从 Web 应用程序传递到 Web API。 Azure AD 身份验证

c# - Chrome 是如何实现其标签的?

c# - 使用 MVVM 模式中的 DXGrid 绑定(bind)/编辑数据的最佳选择是什么?

Silverlight Bing map 图钉样式

c# - 在边框样式中的附加属性上触发。错误: Type initialization failed

c# - ".Add"on Dictionary 以列表作为值

c# - 什么是最好的免费的、与 Ajax.NET (System.Web.Extensions 3.5) 兼容的富文本框控件?

c# - 是否可以将数据上下文的属性绑定(bind)到另一个数据上下文的属性