在 ViewModel 中获取窗口属性 [英] Getting Window Properties in ViewModel

查看:57
本文介绍了在 ViewModel 中获取窗口属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 WPF 应用程序,其中我需要从我的视图模型中获取窗口的宽度、高度和位置.我正在使用以下 XAML:

I'm building a WPF application in which I have a need to get the width, height and locations of the window from my view model. I'm 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>

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

In my view model, the values for the Top, Left work fine, but, the Width and Height properties are both NaN. I have properties like this for each of the attributes I'm binding on:

private double height;

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

知道为什么值会返回 NaN 吗?更重要的是,我该怎么做才能获得我正在寻找的值?

Any idea why the values are coming back NaN? More importantly, what can I do to get the values I'm looking for?

我意识到这有点不对,但我需要从视图模型中捕获应用程序的屏幕截图.视图模型包含我没有粘贴的附加属性,但它们是实现所需功能所必需的.出于好奇,以下是我捕获屏幕的方法:

I realize this is a bit off, but I need to capture a screenshot of the application from the view model. The view model contains additional properties that I didn't paste, but are necessary in order to implement the desired functionality. For the curious, below is my method for capturing the screen:

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
}

推荐答案

正如mzabsky所说,你需要使用ActualWidthActualHeight.这里的问题是它们是只读的,您不能对只读依赖属性执行 OneWayToSource Binding.

As mzabsky said, you need to use ActualWidth and ActualHeight. The problem here is that they are readonly and you can't do a OneWayToSource Binding on a readonly Dependency Property.

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

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

Kent Boogaart 提供的附加行为可让您做到这一点

The attached behavior provided by Kent Boogaart lets you do this

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

或者您可以尝试我为此创建的称为 PushBinding 的解决方案,该解决方案可用于将只读 DP 推送到视图模型.在你的情况下,用法看起来像这样

or you can try the solution I made for this called PushBinding which can be used to push readonly DP's to the viewmodel. In your case the usage would look like this

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

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

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

这篇关于在 ViewModel 中获取窗口属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆