首页 > 解决方案 > How to access Behaviours Property attached to window from ViewModel

问题描述

I have a dependency bool property inside a behavior, which was set in Window to ask the confirmation message popup before closing window. But I don't need this popup when it is being closed from ViewModel. So how can I access the property in the behavior and how to set it as False.

<Window WindowState="Normal" WindowStyle="SingleBorderWindow" ResizeMode="NoResize" 
        behaviors:WndExitAftConfirmBehaviour.Enabled="True"/>

How can I access the Enabled property in ViewModel and how to set it as False?

标签: wpfdependency-properties

解决方案


与其将其设置为常量,True不如将其绑定到视图模型中的布尔属性并将该属性设置为False窗口关闭时。

如果您不使用数据绑定,那么只需执行以下操作:

Application.Current.MainWindow.SetValue(WndExitAftConfirmBehaviour.EnabledProperty, false);

推荐阅读