Skip to content

How to access the control instance from the view model #349

Answered by canton7
zpccode asked this question in Q&A
Discussion options

You must be logged in to vote

The general approach is not to do this: MVVM strong discourages interacting directly with the view from the VM. If you really need to, you can access the View property on the ViewModel, and cast it to your view type.

How exactly you go about it depends on what problem you're solving. For dynamically adding lines to a canvas for example, you would normally have the VM expose a BindableCollection containing a list of objects describing your lines (e.g. start and end point). Then you might be able to bind to that in your XAML, or you might need to bind it to an attached behaviour or read it in the codebehind.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@zpccode
Comment options

@canton7
Comment options

Answer selected by zpccode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants