Getting window properties in ViewModel

I am creating a WPF application in which I need to get the width, height and position of a window from my view model. I am using the following XAML:

<Window x:Class="ScreenCapture.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Height="{Binding Height, Mode=OneWayToSource, FallbackValue=300}" Width="{Binding Width, Mode=OneWayToSource, FallbackValue=300}" Title="MVVM Light Application" Top="{Binding Top, Mode=OneWayToSource}" Left="{Binding Left, Mode=OneWayToSource}" DataContext="{Binding Main, Source={StaticResource Locator}}"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Skins/MainSkin.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid x:Name="LayoutRoot"> <Grid.ColumnDefinitions> <ColumnDefinition Width="13*" /> <ColumnDefinition Width="265*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="25" /> </Grid.RowDefinitions> <TextBlock FontSize="36" FontWeight="Bold" Foreground="Purple" Text="{Binding Welcome}" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" Grid.Column="1" Margin="19,70,32,70" /> <Button Grid.Row="1" Content="Capture" Grid.ColumnSpan="2" Command="{Binding Capture}" /> </Grid> </Window> 

In my view model, the values โ€‹โ€‹for Top, Left work fine, but the Width and Height properties are like NaN. I have properties similar to this for each of the attributes that I attach to:

 private double height; public double Height { set { height = value; } } 

Any idea why the values โ€‹โ€‹are returned by NaN? More importantly, what can I do to get the values โ€‹โ€‹I'm looking for?

EDIT: I understand that this does not work a bit, but I need to take a screenshot of the application from the view model. The view model contains additional properties that I did not insert, but which are necessary to implement the desired functionality. For the curious, below is my screen capture method:

 private void CaptureScreen() { int x = Convert.ToInt32(left); int y = Convert.ToInt32(top); int w = Convert.ToInt32(width); int h = Convert.ToInt32(height); Bitmap image = new Bitmap(w, h); Graphics graphics = Graphics.FromImage(image); graphics.CopyFromScreen(x, y, x, y, new Size(w, h), CopyPixelOperation.SourceCopy); // Do something with the image } 
+8
wpf mvvm xaml
source share
2 answers

As Mzabsky said, you need to use ActualWidth and ActualHeight . The problem here is that they are readonly, and you cannot do OneWayToSource Binding just for the read-only dependency property.

However, there are workarounds.
See the next question: Pushing read-only GUI properties back to ViewModel

The enclosed behavior provided by Kent Boogaart allows you to do this.

 <Window ... SizeObserver.Observe="True" SizeObserver.ObservedWidth="{Binding Width, Mode=OneWayToSource}" SizeObserver.ObservedHeight="{Binding Height, Mode=OneWayToSource}"> 

or you can try the solution that I made for this, called PushBinding , which you can use to put readonly DP into view mode. In your case, usage will look like this

 <Window ...> <pb:PushBindingManager.PushBindings> <pb:PushBinding TargetProperty="ActualHeight" Path="Height"/> <pb:PushBinding TargetProperty="ActualWidth" Path="Width"/> </pb:PushBindingManager.PushBindings> </Window> 

You can download the demo project using PushBinding here if you are interested: https://www.dropbox.com/s/eqkmsp0q7hb568z/PushBindingInStyleDemo.zip?dl=0

+9
source share

It is supposed to use ActualWidth and ActualHeight as anchor targets for one way to snap the source, not in height and width - this is the desired size, not the actual size. Width and height - NaN, unless you specifically set them for a certain value.

Of course, another question is that this does not seem to be the correct use of a view model.

+2
source share

All Articles