简体   繁体   English

WPF UserControl公开自定义属性

[英]WPF UserControl Expose Custom Property

I am developing an application following the MVVM model and writing my code in VB. 我正在开发一个遵循MVVM模型的应用程序,并在VB中编写我的代码。

I have created my own UserControl, let's call it 'Arrow', which has a property in its ViewModel called 'Angle'. 我创建了自己的UserControl,我们称之为'Arrow',它的ViewModel中有一个名为'Angle'的属性。 In my main window I have a ListBox which is bound to a collection and each ListItem will be an Arrow. 在我的主窗口中,我有一个ListBox ,它绑定到一个集合,每个ListItem都是一个Arrow。

How can I bind an angle value in the ListBox 's DataSource to the Angle property of each instance of my UserControl please? 如何将ListBoxDataSource的角度值绑定到UserControl的每个实例的Angle属性?

From my research so far it looks as if I need to create a DependencyProperty for Angle. 从目前为止我的研究看起来好像我需要为Angle创建一个DependencyProperty If this is the case do I create this in my ViewModel, in the code-behind the XAML or elsewhere? 如果是这种情况,我是在我的ViewModel,XAML代码隐藏或其他地方创建的吗?

I'm sure this is simple when you know how! 当你知道怎么做时,我确信这很简单!

The DependencyProperty would be defined in the UserControl (the code-behind). DependencyProperty将在UserControl中定义(代码隐藏)。 You'd then bind that property to the property in the ViewModel 然后,您将该属性绑定到ViewModel中的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM