扩展 MVVM 的现有控件
我正在尝试扩展一个名为 PivotViewer 的现有 Microsoft 控件。
该控件有一个现有属性,我想将其公开给我的 ViewModel。
public ICollection<string> InScopeItemIds { get; }
我创建了一个名为 CustomPivotViewer 的继承类,并且想要创建一个可以绑定的依赖属性,该属性将公开基类中 InScopeItemIds 中保存的值。
我花了很长时间阅读有关 DependencyPropertys 的内容,但我变得非常沮丧。
这可能吗?
I am tring to extend an existing microsoft control called the PivotViewer.
This control has an existing property that I want to expose to my ViewModel.
public ICollection<string> InScopeItemIds { get; }
I have created an inherited class called CustomPivotViewer and I want to create a Dependency Property that I can bind to that will expose the values held in InScopeItemIds in the base class.
I have spent a fair while reading up about DependencyPropertys and am becomming quite disheartened.
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望它可绑定,则只需要一个
DependencyProperty
,这意味着:例如,如果您想在您的应用程序中拥有MyBindableProperty
属性,控件,您希望能够使用它来执行操作:但是,如果您希望其他
DependencyProperties
将绑定到它,则任何属性(无论是可以使用DependencyProperty
或普通的 DependencyProperty。我不确定你真正需要什么,也许你可以澄清更多,但如果这是你想要实现的第一个场景,你可以按如下方式执行:
创建一个
DependencyProperty
,让我们将其命名为BindableInScopeItemIds
,如下所示:在
OnBindableInScopeItemIdsChanged
中,您可以更新内部集合 (InScopeItemIds
)请记住您的属性想要公开的是只读(它没有“setter”),因此您可能需要这样更新:
希望这会有所帮助:)
You only need a
DependencyProperty
is you want it to be bindable, meaning: if you want to have, for example, aMyBindableProperty
property in your control, with which you want to be able to do:if, however, you want other
DependencyProperties
to bind to it, any property (either aDependencyProperty
or a normal one) can be used.I'm not sure what you really need, maybe you can clarify more, but if it's the first scenario that you want to implement, you can do it as follows:
create a
DependencyProperty
, let's call itBindableInScopeItemIds
, like so:in the
OnBindableInScopeItemIdsChanged
, you can update the inner collection (InScopeItemIds
)remember that the property you want to expose is read-only (it has no "setter"), so you might need to update it as so:
Hope this helps :)
编辑:
我意识到了误解,这是一个新版本(在原始问题的上下文中):
因此,您可以使用绑定所需的属性,并考虑以下情况:
EDIT:
I realized misunderstandings and here is a new version (in the context of the original question):
So, you can use the property you need for the binding, with following circumstances having in mind: