I
I
Ignatiy22019-12-20 16:09:42
WPF
Ignatiy2, 2019-12-20 16:09:42

Is it possible to find out the width of the View element in the ViewModel?

For example, I have a button on click on which I want to display its dimensions through the MessageBox. And I want to do this in the ViewModel. But as far as I understand, the ViewModel should not know anything about the View. If so, then where and how can this size inference be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-12-20
@Ignatiy2

Depends on why you need information about the sizes of UI elements.
If this is part of the user settings, then storing them at the ViewModel level is completely normal and does not contradict the pattern in any way.
But if this is needed solely for temporary UI customization, then the code should also be at the View level.
For your example, the solution would be: add a button click event handler and display information there.
MVVM does not mean that there should not be any code in the code-behind. This is a typical mistake. There should not be anything in .xaml.cs that is not directly related to display.
Animation control, display of help messages, customization of user controls - it can all be there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question