绑定"WindowState".使用MVVM在WPF中的窗口的属性 [英] Binding the "WindowState" property of a window in WPF using MVVM

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

问题描述

我将主窗口的"WindowState"属性绑定到ViewModel上,以便通过命令更改窗口的状态,但是第一次我最小化窗口时,它就最小化了工作表,就像Excel文件中的工作表一样.是否可以解决此问题或将"WindowState"属性绑定到我的ViewModel以便窗口正确最小化的正确方法?

I bound the "WindowState" property of my main window to my ViewModel in order to change the state of the window by a command, but the first time I minimize the window it minimizes like a worksheet does in an Excel file. Is there a work around for this or a correct way to bind the "WindowState" property to my ViewModel so that the window minimizes correctly?

推荐答案

我认为您不应该关心视图模型中的窗口状态,这是完全错误的,因为较低级别的层知道较高级别的层层(因此,错误的关注点分离(SOC)).

I don't think you should care about the window state in a view model, it's completely wrong because a lower-level layer is aware of a higher-level layer (thus wrong Separation of Concerns (SOC)).

在这种情况下,我通常要做的是从包含视图模型的控件或窗口(即视图)的代码背后订阅视图模型中的更改.在这种情况下,在代码隐藏中编写代码是有效的,因为它仅在视图中使用(因此,代码隐藏是此逻辑的理想位置,您实际上并不想对其进行单元测试).

What I normally do in this case is subscribe to changes in the view model from the code-behind of the control or window (thus the view) containing the view model. In this case, it is valid to write code in the code-behind because it is only used in the view (and thus the code-behind is the perfect location for this logic, which you really don't want to unit test).

这篇关于绑定"WindowState".使用MVVM在WPF中的窗口的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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