且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以在Windows用户控件上添加DependencyProperty吗?

更新时间:2023-12-06 14:04:58

您可以通过创建一个 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.