我可以在 Windows 用户控件上添加 DependencyProperty 吗?
我正在尝试在 WPF 应用程序中托管 Visio ActiveX 对象。
为此,我创建了一个 Windows 用户控件项目,在其中添加 Visio 对象。然后,该 Windows 用户控件托管在 WindowsFormsHost 对象中的 WPF 用户控件上。
<WindowsFormsHost Name="wfHost" Grid.Row="1">
<wf:VisioUserControl FileNamePath="?"/>
</WindowsFormsHost>
我想要做的是将 FileNamePath 成员的值绑定到定义路径的 TextBox 元素的值。
该项目遵循 MVVM 模式,因此我无法访问 ViewModel 中的 VisioUserControl 对象。
我正在考虑的解决方案是将 FileNamePath 成员绑定到包含路径的 TextBox 的值,但它不是 DependencyProperty看来我无法在 Windows 用户控件后面的代码中定义一个。
那么,是否有任何解决方法来执行此绑定?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过创建一个包含
VisioUserControl
的UserControl
来解决此问题(我写了一个 此处有关用户控件创建的简单教程)。然后,您可以将FileNamePath
依赖属性添加到UserControl
中。在此依赖项属性的属性更改处理程序中,设置此用户控件包装的VisioUserControl
上的FileNamePath
属性。You can solve this by creating a
UserControl
that wraps yourVisioUserControl
(I wrote a simple tutorial on UserControl creation here). You can then add aFileNamePath
dependency property to yourUserControl
. In the property changed handler of this dependency property, set theFileNamePath
property on theVisioUserControl
that this user control wraps.好的,我创建了一个托管 Winforms 控件的 WPF 用户控件示例,其依赖属性绑定到 winforms 控件的文本属性。
这是控件的 xaml(非常简单),
您需要做的是将测试对象从 Label 更改为您正在使用的任何 Visio 对象。然后在属性回调中将文本属性更改为文件名或您想要的任何属性。
如上所述,这是在后面的代码中完成的,但是对于用户控件来说很好,它与使用它的任何东西完全解耦,您只需要绑定到控件的 filename 属性即可。
这是我创建的项目的链接,其中显示了如何使用该控件。有一个文本框,其文本绑定到 FileName 属性,这会更改 Winforms 标签文本。
如果您想在 winforms 中使用它,您可以将其放置在 Winforms Usercontrol 中(就像您在回复我的评论中提到的那样)
尝试替换控件的标签,看看它是否有效。
Ok I have created an example of a WPF usercontrol that is hosting a Winforms control, with a dependency property that is bound to the winforms control's text property.
Here is the xaml of the control (its very simple)
What you need to do is change the test object from a Label to whatever Visio object you were using. Then in the property callback change the text property to the filename or whatever property you wanted.
As mentioned above this is done in the code behind, but that is fine for a user control, its completely decoupled from whatever thing is using it, you just need to bind to the filename property of the control.
Here is a link to a project I created showing how the control is used. There is a textbox whos text is bound to the FileName property, which changes the Winforms Labels text.
You can place this in a Winforms Usercontrol if you want to use it in winforms (like you mentioned in your reply to my comment)
Try replacing the label for your control and see if it works.
为什么不实现 UserControl 来包装 WindowsFormHost 和 Visio 用户控件?然后,您可以添加依赖属性,并在 PropertyChangedCallback 处理程序后面的代码中实现,并适当地与 WinForms 控件交互
Why not implement a UserControl to wrap the WindowsFormHost and the Visio user control? Then you cann add a Dependency Property, and implement in the code behind a handler for the PropertyChangedCallback, and appropiately interact with the WinForms control