开发者

WPF MVVM - trouble moving things to the view model

I am starti开发者_JS百科ng my way with MVVM.. I already implemented a window and need to change it to be mvvm but.. in my window i have a function that searches the visual tree how can i do this in my view model? i cannot access a function in the view from viewmodel..


You can create a custom interface that exposes that function and inject its implementation in your ViewModel.

It might not be the most elegant solution, but it is a quick one, (mock-)testable and loosely-coupled.

Please refer to this answer for a well-written example:

How to play Sound and Animations in MVVM


You don't do that in your ViewModel. Accessing the View should be done in the View and if that requires code an option would be to develop a control that contains the code and use the control in the View.

Do not add any knowledge about the View to the ViewModel. That would mess up the pattern and remove (some of) the benefits of MVVM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜