In the ViewModel to know the width of the View element?

For example, I have a button by clicking on which I want to display MessageBox through its size. And to do that I want in the ViewModel. But I understand that ViewModel shouldn't know anything about the View. If this is so, then where and how you can implement this output size?
April 3rd 20 at 18:49
2 answers
April 3rd 20 at 18:51
Depends on why you need information about the sizes of UI elements.

If it's some user settings, then store them in the ViewModel layer is perfectly normal and does not contradict the pattern.

But if it should be only for temporary settings UI, and the code should be at the level of the View.

For your example the solution is: add event handler to click on button and display the information there.

MVVM this does not mean that code-behind should be no code. This is a common mistake. In .xaml.cs should not be that does not directly relate to the display.

The animation controller, the output of the subsidiary messages, customization of custom controls can all be there.
April 3rd 20 at 18:53
I do not know how this is contrary to the pattern, but with the move come to a decision.

<Button x:Name="MyButton" Command="{Binding ClickCommand}" 
CommandParameter="{Binding ElementName=MyButton}"/>

public class MainViewModel : BaseViewModel
 public Command ClickCommand { get; set; }

 public MainViewModel()
 ClickCommand = new Command(ClickCommandExecute);

 private void ClickCommandExecute(object obj)
 Button button = obj as Button;
 MessageBox.Show(button.ActualHeight.ToString() + " : "+ button.ActualWidth);

Just hush! ViewModel word!
If ViewModel made in a separate Assembly, then there is no references of the controls. Because ViewModel not tied to a view (views may be somewhat different). Therefore, it is better to make a request from ViewModel in the presentation through the event, in the representation of (in .xaml.cs) subscribe to the event
EventHandler<ButtonHeightEventArgs> MyButtonHeightRequest
and there to fill in the property argument. - tony_Turner8 commented on April 3rd 20 at 18:56

Find more questions by tags WPFC#