<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>the art of code</title>
	<atom:link href="http://theartofcode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://theartofcode.com</link>
	<description>creating clean software without going crazy</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:10:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='theartofcode.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>the art of code</title>
		<link>http://theartofcode.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://theartofcode.com/osd.xml" title="the art of code" />
	<atom:link rel='hub' href='http://theartofcode.com/?pushpress=hub'/>
		<item>
		<title>Closing a WPF window from a view model.</title>
		<link>http://theartofcode.com/2011/04/18/closing-a-wpf-window-from-a-view-model/</link>
		<comments>http://theartofcode.com/2011/04/18/closing-a-wpf-window-from-a-view-model/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 04:14:02 +0000</pubDate>
		<dc:creator>Dan Graham</dc:creator>
				<category><![CDATA[MVVM]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://theartofcode.com/?p=8</guid>
		<description><![CDATA[So you&#8217;ve embraced using WPF and the MVVM model, and you may have already been impressed with how easy it is to create powerful user interfaces.  But as with most technologies or patterns sometimes it&#8217;s not obvious how to accomplish even &#8230; <a href="http://theartofcode.com/2011/04/18/closing-a-wpf-window-from-a-view-model/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=theartofcode.com&amp;blog=22270254&amp;post=8&amp;subd=theartofcodedotcom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve embraced using WPF and the MVVM model, and you may have already been impressed with how easy it is to create powerful user interfaces.  But as with most technologies or patterns sometimes it&#8217;s not obvious how to accomplish even the simplest tasks.</p>
<p>Case and point, you have a wpf window that represents a view and a command in that view model that is supposed to close that view.  In the MVVM pattern, the viewmodel is unaware of any view (in this case a window) that it is presenting.  It doesn&#8217;t have a reference to the window, so it can&#8217;t simply call &#8220;window.Close()&#8221;.</p>
<p>You could put and event in your view model, that the window could suscribe to that event with a handler that closes the window.  It works, but it&#8217;s not really consistant with the MVVM pattern.  When using WPF and MVVM, the view only ever receives notification that a property has changed.  So to signal the window to close, all it really needs is to know that a property has changed.</p>
<p>Now, it would be nice if there was a property on a window that could be bound to the view model&#8230; a property like IsOpen&#8230;   Fortunatly with WPF, one can add properties to any view.  Thanks to attached dependancy properties.</p>
<p>On his excellent WPF blog Josh Smith talks about the concept of using attached dependancy properties, to attach behaviors to a control that wouldn&#8217;t otherwise behave that way.  Using this same approach, we can create an attached property for a window that can be bound.</p>
<p><pre class="brush: csharp;">

    public static class WindowBehaviors
    {
        public static readonly DependencyProperty IsOpenProperty =
                 DependencyProperty.RegisterAttached(&quot;IsOpen&quot;, typeof(bool), typeof(WindowBehaviors),
                 new PropertyMetadata(IsOpenChanged));

        private static void IsOpenChanged(DependencyObject obj,
                                          DependencyPropertyChangedEventArgs args)
        {
            Window window = Window.GetWindow(obj);

            if (window != nulll &amp;&amp; !((bool)args.NewValue))
                window.Close();
        }

        public static bool GetIsOpen(Window target)
        {
            return (bool)target.GetValue(IsOpenProperty);
        }

        public static void SetIsOpen(Window target, bool value)
        {
            target.SetValue(IsOpenProperty, value);
        }
    }
</pre></p>
<p>With the dependancy property created, you can now use the bindable IsOpen property on your window:<br />
<pre class="brush: xml;">
&lt;Window ...
        xmlns:wb=&quot;clr-namespace:WpfApplication&quot; 
        wb:WindowBehaviors.IsOpen=&quot;{Binding IsOpen}&quot;
        ... 
</pre></p>
<p>and just like that, when the IsOpen property of the view model is set to false, the window will close.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/theartofcodedotcom.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/theartofcodedotcom.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/theartofcodedotcom.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/theartofcodedotcom.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/theartofcodedotcom.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/theartofcodedotcom.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/theartofcodedotcom.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/theartofcodedotcom.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=theartofcode.com&amp;blog=22270254&amp;post=8&amp;subd=theartofcodedotcom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2011/04/18/closing-a-wpf-window-from-a-view-model/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/41bda41eb4a36bb5b70790607cb43b38?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">themaninblack173</media:title>
		</media:content>
	</item>
	</channel>
</rss>
