当前位置: 移动技术网 > IT编程>开发语言>.net > WPF通过附加属性控制窗口关闭

WPF通过附加属性控制窗口关闭

2018年11月03日  | 移动技术网IT编程  | 我要评论

3jp手机电影下载,非你莫属20130420,挂烫机

场景1

当使用 showdialog() 方式显示窗口时,通过定义附加属性的方式可实现在 viewmodel 中进行数据绑定(bool?)来控制子窗口的显示和关闭

public class exwindow
{
    public static bool? getdialogresult(dependencyobject obj)
    {
        return (bool?)obj.getvalue(dialogresultproperty);
    }

    public static void setdialogresult(dependencyobject obj, bool value)
    {
        obj.setvalue(dialogresultproperty, value);
    }

    // using a dependencyproperty as the backing store for dialogresult.  this enables animation, styling, binding, etc...
    public static readonly dependencyproperty dialogresultproperty =
        dependencyproperty.registerattached("dialogresult", typeof(bool?), typeof(exwindow), new propertymetadata(true, (d, e) =>
        {
            var handler = d as window;
            handler.dialogresult = e.newvalue as bool?;
        }));
}

场景2

当主窗口的显示和关闭也想通过在 viewmodel 中来进行控制的话可以通过事件和消息级制来实现,具体可参考 mvvm light 中的 messenger 使用方式

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网