我可以在Windows用户控件上添加DependencyProperty吗? [英] Can I add a DependencyProperty on an windows user control?

查看:140
本文介绍了我可以在Windows用户控件上添加DependencyProperty吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在WPF应用程序中托管一个Visio ActiveX对象。



为此,我创建了一个Windows用户控件项目,其中添加了Visio对象。这个Windows用户控件然后托管在WindowsFormsHost对象中的WPF用户控件上。

 < WindowsFormsHost Name =wfHostGrid .Row =1> 
< wf:VisioUserControl FileNamePath =?/>
< / WindowsFormsHost>

我想做的是绑定 FileNamePath 的值成员为定义路径的 TextBox 元素的值。



项目遵循MVVM模式,所以没有办法访问我的ViewModel中的 VisioUserControl 对象。



我正在考虑的解决方案是将 FileNamePath 成员绑定到包含路径的 TextBox 的值,但它不是DependencyProperty ,似乎我无法在Windows用户的代码中定义一个



所以,是否有解决方法来执行此绑定?



提前感谢。 >

解决方案

您可以通过创建一个 UserControl 来解决这个问题, c> VisioUserControl (我写了一个简单的教程,在这里创建UserControl创建)。然后,您可以将 FileNamePath 依赖属性添加到您的 UserControl 中。在该依赖属性的属性更改处理程序中,在该用户控件所包含的 VisioUserControl 上设置 FileNamePath 属性。 / p>

I'm trying to host a Visio ActiveX object in a WPF application.

To do this, I created a Windows user control project where I add the Visio object. This windows user control is then hosted on an WPF user control in an WindowsFormsHost object.

<WindowsFormsHost Name="wfHost" Grid.Row="1">
    <wf:VisioUserControl FileNamePath="?"/>
</WindowsFormsHost>

What I would like to do is to bind the value of the FileNamePath member to the value of a TextBox element which defines the path.

The project follows the MVVM pattern, so there is no way that I can access the VisioUserControl object in my ViewModel.

The solution I was thinking about is to bind the FileNamePath member to the value of the TextBox that contains the path, but it is not a DependencyProperty and it seems that I'm not able to define one in the code behind of the windows user control.

So, is there any workaround to perform this binding?

Thanks in advance.

解决方案

You can solve this by creating a UserControl that wraps your VisioUserControl (I wrote a simple tutorial on UserControl creation here). You can then add a FileNamePath dependency property to your UserControl. In the property changed handler of this dependency property, set the FileNamePath property on the VisioUserControl that this user control wraps.

这篇关于我可以在Windows用户控件上添加DependencyProperty吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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