在ViewModel中获取窗口属性

sen*_*nfo 8 wpf xaml mvvm

我正在构建一个WPF应用程序,我需要从我的视图模型中获取窗口的宽度,高度和位置.我正在使用以下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>
Run Code Online (Sandbox Code Playgroud)

在我的视图模型中,Top,Left的值工作正常,但是Width和Height属性都是NaN.对于我绑定的每个属性,我都有这样的属性:

private double height;

public double Height
{
    set
    {
        height = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

知道为什么价值回归NaN?更重要的是,我能做些什么来获得我正在寻找的价值?

编辑:我意识到这有点偏,但我需要从视图模型中捕获应用程序的屏幕截图.视图模型包含我未粘贴的其他属性,但是为了实现所需的功能是必需的.对于好奇,下面是我捕获屏幕的方法:

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
}
Run Code Online (Sandbox Code Playgroud)

Fre*_*lad 10

正如mzabsky所说,你需要使用ActualWidthActualHeight.这里的问题是它们只是readonly而你不能OneWayToSource Binding在readonly Dependency Property上做.

但是,有一些解决方法.
请参阅以下问题:将只读GUI属性推送回ViewModel

Kent Boogaart提供的附加行为允许您执行此操作

<Window ...
        SizeObserver.Observe="True"
        SizeObserver.ObservedWidth="{Binding Width, Mode=OneWayToSource}"
        SizeObserver.ObservedHeight="{Binding Height, Mode=OneWayToSource}">
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试我为此制作的解决方案,该解决方案PushBinding可用于将只读DP推送到viewmodel.在您的情况下,使用情况将如下所示

<Window ...>
    <pb:PushBindingManager.PushBindings> 
        <pb:PushBinding TargetProperty="ActualHeight" Path="Height"/> 
        <pb:PushBinding TargetProperty="ActualWidth" Path="Width"/>
    </pb:PushBindingManager.PushBindings> 
</Window> 
Run Code Online (Sandbox Code Playgroud)

PushBinding如果您有兴趣,可以使用此处下载演示项目:https://www.dropbox.com/s/eqkmsp0q7hb568z/PushBindingInStyleDemo.zip?dl = 0