<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Mark's Blog of Random Thoughts</title>
  <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/" />
  <link rel="self" href="http://www.julmar.com/blog/mark/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2009-06-29T09:51:35.6379786-07:00</updated>
  <author>
    <name>Mark Smith</name>
  </author>
  <subtitle>Wandering through the Wilderness</subtitle>
  <id>http://www.julmar.com/blog/mark/</id>
  <generator uri="http://dasblog.info/" version="2.3.9074.18820">DasBlog</generator>
  <entry>
    <title>Menus and MVVM (2)</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/04/29/MenusAndMVVM2.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,f096a7d7-fdae-4e7b-ab43-70dadf817800.aspx</id>
    <published>2009-04-29T06:30:51.278-07:00</published>
    <updated>2009-04-29T07:08:04.7570279-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the previous post, I wrote about using the MVVM pattern with Context Menus, but
you can use it with top-level menus too -- for example, perhaps I'd like to manage
a set of options through a set of checkboxes.  First, let's start with my list
of options - for simplicity, we'll just make it an enumeration:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">enum</span> Test<br />
{<br />
   A,<br />
   B,<br />
   C,<br />
   D,<br />
   E,<br />
   F<br />
}</span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">Next,
let's define a ViewModel to wrap this - each entry should support a single value
and a boolean binding to determine whether it is checked:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> CheckValues<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span> Test Value { get; set; }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> Text
{ get { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> Value.ToString();
} }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">bool</span> IsChecked
{ get; set; }<br />
}<br /></span>
          </p>
        </span>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <font face="Verdana" color="#003300" size="2">Very
likely this object would also implement INotifyPropertyChanged and fire change notifications,
in this case it's not necessary since we aren't changing the values from code behind. 
The next step is to expose this as a bindable collection - since it's not changing
we'll just use a <strong>List&lt;T&gt;</strong>.</font>
            </span>
          </p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">
              <p>
                <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public
List&lt;CheckValues&gt; EnumValues { get; set; }<br /></span>
              </p>
              <p>
                <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">...
</span>
              </p>
            </font>
          </span>
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <font color="#000000">EnumValues </font>=</span>
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> List&lt;CheckValues&gt;();<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">foreach</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> t <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> Enum.GetValues(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">typeof</span> (Test)))<br />
{<br />
   EnumValues.Add(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> CheckValues
{Value <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (Test)
t});<br />
}</span>
          </p>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">Finally,
in the XAML itself we want to databind to the collection of CheckValues, supplying
the appropriate support for each MenuItem to bind the menu checks to the collection:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <font face="Verdana" color="#003300" size="2">
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;Menu
DockPanel.Dock="<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Top</span>"&gt;<br />
   &lt;MenuItem Header="Enum <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Values</span>"
ItemsSource="{Binding EnumValues}"&gt;<br />
      &lt;MenuItem.ItemContainerStyle&gt;<br />
         &lt;Style TargetType="MenuItem"&gt;<br />
            &lt;Setter Property="Header"
Value="{Binding <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>}"
/&gt;<br />
            &lt;Setter Property="IsCheckable"
Value="True" /&gt;<br />
            &lt;Setter Property="IsChecked"
Value="{Binding IsChecked}" /&gt;<br />
         &lt;/Style&gt;<br />
      &lt;/MenuItem.ItemContainerStyle&gt;<br />
   &lt;/MenuItem&gt;<br />
&lt;/Menu&gt;</span>
            </p>
          </font>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">That
gives us our nice "checkable" menu - when the user clicks a checkmark, it sets the
appropriate boolean in the code behind which could trigger logic, set values, etc.</font>
          </span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">But
wait!  Often with checkboxes you want mutual exclusive choices - like the RadioButton
groups.  This was a feature of Windows Forms which unfortunately did not make
it into WPF, but we can make it work that way.  We could use the above logic
and have the selection of a specific CheckValue unselect any others, or we could take
advantage of RadioButtons to do this -- unfortunately I've not found a way that doesn't
invoice a little code behind so it's not quite as clean as I'd like it to be, but
it is possible.</font>
          </span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">First,
let's define an exclusive ViewModel object, here we will track the "current" selection:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> ExclusiveCheckValues<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span> Test
SelectedValue { get; set; }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span> Test Value { get; set; }<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> Text
{ get { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> Value.ToString();
} }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">bool</span> IsChecked<br />
   {<br />
      get { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> Value
== SelectedValue; }<br />
      set<br />
      {<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">        
if</span> (value)<br />
            SelectedValue <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Value;<br />
      }<br />
   }<br />
}<br /></span>
          </p>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">Next,
we populate it into a bindable collection just as before and then we create some slightly
more complex XAML for our menu items.  The trick here is to put a RadioButton
into the menu item and associate it with a group.  I have to give a shout out
to Dr. WPF from the wpf-disciples list some kudos here - he posted this trick on the
MSDN forums (<a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/76c14163-9462-44d6-b0eb-6f22a006b423">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/76c14163-9462-44d6-b0eb-6f22a006b423</a>). 
It turns out the MenuItem.Icon is positioned just where the normal check mark would
be so if we place a styled RadioButton as the MenuItem.Icon we can make it look just
like the checkmark:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;Menu
DockPanel.Dock="<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Top</span>"&gt;<br /><br />
   &lt;Menu.Resources&gt;<br />
      &lt;RadioButton x:<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Key</span>="rb"
x:Shared="false" HorizontalAlignment="<span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Left</span>" 
<br />
                  
GroupName="MenuGroup" IsChecked="{Binding IsChecked}"&gt;<br />
         &lt;RadioButton.Template&gt;<br />
            &lt;ControlTemplate
TargetType="{x:Type RadioButton}"&gt;<br />
              
&lt;Path x:Name="<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">check</span>"
Margin="7,0,0,0" Visibility="Collapsed" VerticalAlignment="Center"<br />
                     
Fill="{TemplateBinding MenuItem.Foreground}" FlowDirection="LeftToRight"<br />
                     
Data="M 0,5.1 L 1.7,5.2 L 3.4,7.1 L 8,0.4 L 9.2,0 L 3.3,10.8 Z"/&gt;<br />
              
&lt;ControlTemplate.Triggers&gt;<br />
                  
&lt;<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Trigger</span> Property="IsChecked"
Value="True"&gt;<br />
                     
&lt;Setter TargetName="<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">check</span>"
Property="Visibility" Value="Visible" /&gt;<br />
                  
&lt;/<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Trigger</span>&gt;<br />
                &lt;/ControlTemplate.Triggers&gt;<br />
             &lt;/ControlTemplate&gt;<br />
          &lt;/RadioButton.Template&gt;<br />
       &lt;/RadioButton&gt;<br />
    &lt;/Menu.Resources&gt;<br /><br />
    &lt;MenuItem Header="Single <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Select</span> Enum <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Values</span>"
ItemsSource="{Binding ExclusiveEnumValues}"&gt;<br />
       &lt;MenuItem.ItemContainerStyle&gt;<br />
          &lt;Style TargetType="MenuItem"&gt;<br />
             &lt;Setter
Property="Header" Value="{Binding <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>}"
/&gt;<br />
             &lt;Setter
Property="Icon" Value="{DynamicResource rb}" /&gt;<br />
             &lt;EventSetter
Event="Click" Handler="OnMenuItemClick" /&gt;<br />
          &lt;/Style&gt;<br />
       &lt;/MenuItem.ItemContainerStyle&gt;<br />
    &lt;/MenuItem&gt;<br />
&lt;/Menu&gt;</span>
          </p>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">Here,
we've placed the RadioButton into our resources - but marked it as non-shareable. 
The issue is that Styles always share property setters - and if you tried to do this:</font>
          </span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">
              <font face="Courier New">
                <font color="#000000">&lt;Style
TargetType="MenuItem"&gt;<br />
   &lt;Setter Property="Icon"</font>
                <font color="#000000">&gt;<br />
     &lt;Setter.Value&gt;<br />
       &lt;RadioButton ... /&gt;<br />
     &lt;/Setter.Value&gt;<br />
   &lt;/Setter&gt;</font>
              </font>
            </font>
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">
              <br />
            </font>
          </span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">You
will get a very strange error message: "Cannot assigned type 'RadioButton' to type
'object'".  Which makes absolutely no sense until you realize the XAML parser
is trying to assign the <strong>same</strong> RadioButton to every MenuItem.Icon. 
The non-shareable resource fixes that issue.  The last step is to set the check
-- this is done through the EventSetter in the above template.  It's wired to
the little bit of code behind:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> OnMenuItemClick(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
RoutedEventArgs e)<br />
{<br />
   ((RadioButton) ((MenuItem) sender).Icon).IsChecked <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">true</span>;<br />
}<br /></span>
          </p>
        </span>
        <p>
          <font face="Verdana" color="#003300" size="2">This then sets the IsChecked property
which will, in turn, select that item.  Now you can look at <strong>ExclusiveCheckValue.SelectedValue</strong> to get
the current item.  This isn't the most elegant solution, nor is it the only solution
but it's an interesting one to think on.  If I were truly building this into
a production application, I'd probably look at controlling the selection in the ModelView
(i.e. enumerate through my other choices and deliberately turn them off). 
But this does illustrate how powerful WPF is! </font>
        </p>
        <p>
          <font face="Verdana" color="#003300" size="2">
            <a href="http://www.julmar.com/samples/menucheckbox.zip">Here's
the example project with both styles.</a>
          </font>
        </p>
        <p>
          <br />
 
</p>
        <img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=f096a7d7-fdae-4e7b-ab43-70dadf817800" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Using MVVM with Menus in WPF</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/04/21/UsingMVVMWithMenusInWPF.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,4c8fb326-39fc-4ef2-8872-8128cc329bd9.aspx</id>
    <published>2009-04-21T15:35:59.426-07:00</published>
    <updated>2009-05-14T10:50:17.3589045-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
One question I've fielded a couple of times is how to manage menus, primarily context
menus, with the MVVM pattern.  It turns out to be pretty easy once you know the
"trick".  The key thing to keep in mind is that menus are just <strong>ItemsControls</strong> -
they support data templating and binding like any other ItemsControl.  However,
the part where people get lost is in hooking up the commands.  Here's the trick:
</p>
        <p>
          <strong>Step 1:</strong> Define some code behind construct to manage each menu item
in a hiearchial fashion.  Here's one I've used:
</p>
        <font size="4">
          <p>
          </p>
        </font>
        <font color="#0000ff">
          <font color="#0000ff">public</font>
        </font>
        <font color="#0000ff">
          <font color="#0000ff">class</font>
        </font>
        <font color="#2b91af">
          <font color="#2b91af">MenuItem<br /></font>
        </font>{<br />
   <font color="#0000ff"><font color="#0000ff">public</font></font><font color="#0000ff"><font color="#0000ff">string</font></font> Text
{ <font color="#0000ff"><font color="#0000ff">get</font></font>; <font color="#0000ff"><font color="#0000ff">set</font></font>;
}<br />
   <font color="#0000ff"><font color="#0000ff">public</font></font><font color="#2b91af"><font color="#2b91af">List</font></font>&lt;<font color="#2b91af"><font color="#2b91af">MenuItem</font></font>&gt;
Children { <font color="#0000ff"><font color="#0000ff">get</font></font>; <font color="#0000ff"><font color="#0000ff">private</font></font><font color="#0000ff"><font color="#0000ff">set</font></font>;
}<br />
   <font color="#0000ff"><font color="#0000ff">public</font></font><font color="#2b91af"><font color="#2b91af">ICommand</font></font> Command
{ <font color="#0000ff"><font color="#0000ff">get</font></font>; <font color="#0000ff"><font color="#0000ff">set</font></font>;
}<br />
   
<br />
   <font color="#0000ff"><font color="#0000ff">public</font></font> MenuItem(<font color="#0000ff"><font color="#0000ff">string</font></font> item) 
<br />
   {<br />
       Text = item; 
<br />
       Children = <font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">List</font></font>&lt;<font color="#2b91af"><font color="#2b91af">MenuItem</font></font>&gt;();<br />
   }<br />
}
<span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><p></p></span></span><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font face="Verdana"><font size="2"><strong>Step
2:</strong> Create your menu structure using the above container.</font></font><p><font face="Verdana" size="2">This involves just creating a <strong>List&lt;MenuItem&gt;</strong> which
holds the root nodes and using a <strong>DelegatingCommand</strong> to wire up some
code behind method.  Here's an example:</font></p><font face="Verdana" size="2"><font size="4"><p></p></font><font color="#0000ff"><font color="#0000ff">public</font></font><font color="#2b91af"><font color="#2b91af">List</font></font>&lt;<font color="#2b91af"><font color="#2b91af">MenuItem</font></font>&gt;
MenuOptions<br />
{<br />
    <font color="#0000ff"><font color="#0000ff">get  </font></font>{
<p><font color="#0000ff"><font color="#0000ff">       var</font></font> menu
= <font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">List</font></font>&lt;<font color="#2b91af"><font color="#2b91af">MenuItem</font></font>&gt;();<br />
       <font color="#0000ff"><font color="#0000ff">if</font></font> (SupportedFileFormats.Count
&gt; 0)<br />
      {<br />
          <font color="#0000ff"><font color="#0000ff">var</font></font> mi
= <font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">MenuItem</font></font>(<font color="#a31515"><font color="#a31515">"O_pen"</font></font>);<br />
          <font color="#0000ff"><font color="#0000ff">foreach</font></font>(<font color="#0000ff"><font color="#0000ff">var</font></font> fl <font color="#0000ff"><font color="#0000ff">in</font></font> SupportedFileFormats)<br />
          {<br />
               var
sff = fl;<br />
              
mi.Children.Add(<font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">MenuItem</font></font>(fl.Attributes.Description) <br />
                      {
Command = <font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">DelegatingCommand</font></font>(()
=&gt; { LoadFromFormat(sff); })});<br />
          }<br />
          menu.Add(mi);<br />
       }
</p><p>
       menu.Add(<font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">MenuItem</font></font>(<font color="#a31515"><font color="#a31515">"Close
_All"</font></font>) { Command = <font color="#0000ff"><font color="#0000ff">new</font></font><font color="#2b91af"><font color="#2b91af">DelegatingCommand</font></font>(OnCloseAll,
() =&gt; FileList.Count &gt; 0)});<br />
       <font color="#0000ff"><font color="#0000ff">return</font></font> menu;<br />
    }
</p><p>
}
</p></font></span><p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font face="Verdana" color="#003300" size="2">Note
the use of an anonymous method to suck the correct file format into the command handler. 
A nice trick so the command is executed with some contextual information of what you
clicked on.</font></span></p><p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font face="Verdana" color="#003300" size="2">Finally,
you need to setup the context menu style:</font></span></p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a31515" size="4"><font color="#a31515" size="4"><p></p></font></font><strong><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Style</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> x</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">:</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4">Key</font></font></strong><strong><font color="#0000ff" size="4"><font color="#0000ff" size="4">="ContextMenuItemStyle"&gt;<br />
   </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Setter</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Property</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="MenuItem.Header"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Value</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Binding</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Text</font></font></strong><strong><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"/&gt;<br />
   </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Setter</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Property</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="MenuItem.ItemsSource"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Value</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Binding</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Children</font></font></strong><strong><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"/&gt;<br />
   </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Setter</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Property</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="MenuItem.Command"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Value</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Binding</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Command</font></font></strong><strong><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"
/&gt;<br /></font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;/</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Style</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&gt;</font></font></strong><p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font face="Verdana" color="#003300" size="2">And
then use the style where you want the menu to appear:</font></span></p><font color="#a31515" size="4"><font color="#a31515" size="4"></font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4"></font></font><p><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">StackPanel</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Orientation</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="Horizontal"&gt;<br />
    </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Image</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Source</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Binding</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Image</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Width</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="16"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Height</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="16"
/&gt;<br />
    </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">TextBlock</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Margin</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="5"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> HorizontalAlignment</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="Left"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> VerticalAlignment</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="Center"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Text</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Binding</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> Header</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"
/&gt;<br />
    </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">StackPanel.ContextMenu</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&gt;<br />
       </font></font><strong><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">ContextMenu</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> ItemContainerStyle</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">StaticResource</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> ContextMenuItemStyle</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> ItemsSource</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">="{</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">Binding</font></font><font color="#ff0000" size="4"><font color="#ff0000" size="4"> MenuOptions</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">}"</font></font><font size="4"></font></strong><font color="#0000ff" size="4"><font color="#0000ff" size="4"><strong>/&gt;<br /></strong>    </font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;/</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">StackPanel.ContextMenu</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&gt;<br /></font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;/</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">StackPanel</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&gt;
</font></font></p><font color="#a31515" size="4"><font color="#a31515" size="4"><p></p></font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&lt;/</font></font><font color="#a31515" size="4"><font color="#a31515" size="4">HierarchicalDataTemplate</font></font><font color="#0000ff" size="4"><font color="#0000ff" size="4">&gt;
<p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font face="Verdana" color="#003300" size="2">Now
the context menu is populated and properly dispatches to your ViewModel commands!</font></span></p><p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font face="Verdana" color="#003300" size="2"><strong>Edit:</strong> added
intermediate object in LINQ query to fix deferred query issue pointed out in comments.</font></span></p></font></font><br />
 
</span><img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=4c8fb326-39fc-4ef2-8872-8128cc329bd9" /></div>
    </content>
  </entry>
  <entry>
    <title>More MVVM love</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/04/21/MoreMVVMLove.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,a53ec32e-5190-411e-9950-dfc3aedadb7e.aspx</id>
    <published>2009-04-21T12:29:47.183-07:00</published>
    <updated>2009-04-21T12:30:41.6835938-07:00</updated>
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recently wrote about the mini-library I tend to use, but <b>Bill Kempf</b> (one
of the WPF disciples) released a MVVM library for WPF called <b>Onyx</b> which looks
very appealing. I especially like his use of static reflection to implement <b>INotifyPropertyChanged</b>.
Sweet Bill! Thanks for sharing! 
</p>
        <br />
        <p>
Check it out: <a href="http://www.codeplex.com/wpfonyx">WPF Onyx</a><img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=a53ec32e-5190-411e-9950-dfc3aedadb7e" /></p>
      </div>
    </content>
  </entry>
  <entry>
    <title>WPF MVVM Helper Library (WPF + MVVM = testability)</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/04/17/WPFMVVMHelperLibraryWPFMVVMTestability.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,8b3e4279-70a5-431e-8fa3-4c1e047df311.aspx</id>
    <published>2009-04-17T09:06:09.056-07:00</published>
    <updated>2009-04-29T06:35:11.5928493-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="html">&lt;p&gt;
There's been a lot of talk about the Model-View-ViewModel pattern recently and it's
usage around the WPF and Silverlight technology stack.&amp;nbsp; When teaching WPF, I
always introduce students to MVVM as part of the Essential WPF class, it's an incredibly
useful pattern that really separates the UI from the code behind behavior.&amp;nbsp; One
of the things I give the students is a library to do MVVM - I also use it in my consulting
work.&amp;nbsp; With all the focus on it lately, I figured maybe it's time to release
it to the public.
&lt;/p&gt;
&lt;p&gt;
A bit of history -- the library is really just a place where I dump all kinds of useful
utility classes, helpers, wrappers, etc. that I tend to use a lot.&amp;nbsp; I started
it about 3 years ago and&amp;nbsp;it wasn't originally intended to be just an MVVM implementation
so&amp;nbsp;you'll find it's got all kinds of stuff in it, not all of which is MVVM specific.&amp;nbsp;
It's evolution owes a lot to various blog posts, WPF Disciples, and other WPF leaders;
I certainly didn't invent anything radically new but borrowed heavily from all kinds
of places as I built various classes I needed for my own work.&amp;nbsp; These classes
tended to evolve with new functionality (either due to necessity, or because a good
idea occurred to me or someone else).&amp;nbsp;For example, there was a recent thread
on the Mediator pattern (initiated by Marlon Grech and added on by Josh Smith, Laurent
Bugnion and others); I already had a message mediator in place but the idea of using
an attribute to hook it up was a great one that I adopted into my library just because
I like the idea.&amp;nbsp; The Delegating command pattern is one you see in a lot of places
- including the Prism implementation.&amp;nbsp; The event routing attached behavior was
made possible by a couple of blog posts by Mike Hilberg and John Gossman.&amp;nbsp;So,
be aware that as you use this code, it owes a lot to a variety of people.&amp;nbsp; That
said, any bugs or issues are completely mine and I take full credit for them.
&lt;/p&gt;
&lt;p&gt;
So, what all is here?&amp;nbsp; Well, quite a bit.&amp;nbsp; As I said, this is a collection
of helpers I've built and reused over the years doing WPF consulting and instruction.&amp;nbsp;
When MVVM came to my notice I worked on trying to completely separate the XAML side
so I'll focus on those classes here.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
The basic idea is to derive your ViewModel classes from one of three base classes
depending on what you need:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;JulMar.Windows.Mvvm.ViewModel&lt;/strong&gt; - supports basic &lt;strong&gt;INotifyPropertyChanged&lt;/strong&gt; and
Close/Activate eventing to the view.&lt;br&gt;
&lt;strong&gt;JulMar.Windows.Mvvm.ValidatingViewModel&lt;/strong&gt; - supports everything above,
adds Validation through &lt;strong&gt;IDataErrorInfo&lt;/strong&gt;&amp;nbsp;support&lt;br&gt;
&lt;strong&gt;JulMar.Windows.Mvvm.EditingViewModel&lt;/strong&gt; - supports basic + validation
+ &lt;strong&gt;IEditableObject&lt;/strong&gt;
&lt;br&gt;
&lt;br&gt;
Next, in each view (XAML) you&amp;nbsp;set the DataContext property to your view model,
the library has a MarkupExtension to do the work for you - 
&lt;/p&gt;
&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt; 
&lt;p&gt;
&amp;lt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Window&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; x&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;Class&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="TestMvvm.MainWindow" 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;xmlns&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="&lt;a href="http://www.julmar.com/wpfhelpers"&gt;http://www.julmar.com/wpfhelpers&lt;/a&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;&amp;nbsp;
xmlns&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;me&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="clr-namespace:TestMvvm"&lt;br&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;DataContext&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;ViewModelCreator&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; ViewModelType&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;={&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;x&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Type&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; me&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;WinViewModel&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}}"&amp;gt;&gt;
&lt;p&gt;
&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;
&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;
&lt;/p&gt;
&gt;&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
This creates the view and also wires up support for closing the view and activating
the view through the ViewModel class (using the &lt;strong&gt;RaiseCloseRequest&lt;/strong&gt; and &lt;strong&gt;RaiseActivateRequest&lt;/strong&gt; methods).
&lt;/p&gt;
&lt;p&gt;
Everything is driven off ICommand - you can bind commands to the lifetime of the view
(so you can detect activation, deactivation, loading, closing) through the &lt;strong&gt;LifetimeEvents&lt;/strong&gt; attached
behavior:
&lt;/p&gt;
&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt; 
&lt;p&gt;
&amp;lt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Window&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; x&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;Class&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="TestMvvm.MainWindow"&lt;br&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;&amp;nbsp;&amp;nbsp; julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;LifetimeEvent.Activated&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; ActivatedCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;LifetimeEvent.Close&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; CloseCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;LifetimeEvent.Loaded&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; LoadedCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt;LifetimeEvent.Deactivated&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; DeactivatedCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"
&amp;gt;&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font size=4&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;/font&gt; 
&lt;p&gt;
These execute the bound command in the ViewModel when those events occur in the view.&amp;nbsp;
If you need other events styles you can use the EventCommander attached behavior which
allows any arbitrary event to be wired to a command.&amp;nbsp; This can be placed on any
UIElement:
&lt;/p&gt;
&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt; 
&lt;p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;EventCommander.Mappings&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;CommandEvent&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Command&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; MouseEnterCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Event&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="MouseEnter"
/&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;CommandEvent&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Command&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; MouseLeaveCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Event&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="MouseLeave"
/&amp;gt;&lt;br&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&lt; FONT&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;EventCommander.Mappings&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;gt;&gt;
&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;
&lt;font color=#000000 size=2&gt;You can also wire up keyboard and mouse gestures to commands
using the &lt;strong&gt;InputBinder&lt;/strong&gt; attached property:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;InputBinder.Bindings&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;gt;&lt;br&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;KeyBinding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Command&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; ExitCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Key&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="F3"&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Modifiers&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="ALT"
/&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;MouseBinding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Command&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="{&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;Binding&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; ExitCommand&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;}"&lt;/font&gt;&lt;/font&gt;&lt;font color=#ff0000 size=4&gt;&lt;font color=#ff0000 size=4&gt; Gesture&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;="Control+RightClick"
/&amp;gt;&lt;br&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&lt; FONT&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;julmar&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;:&lt;/font&gt;&lt;/font&gt;&lt;font color=#a31515 size=4&gt;&lt;font color=#a31515 size=4&gt;InputBinder.Bindings&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=4&gt;&lt;font color=#0000ff size=4&gt;&amp;gt;
&lt;/p&gt;
&lt;/font&gt;&gt; 
&lt;p&gt;
&lt;font color=#000000 size=2&gt;This replaces the traditional &lt;strong&gt;InputBindings&lt;/strong&gt; collection
with a data bindable version - it also supports &lt;strong&gt;CommandParameter&lt;/strong&gt; objects
on each &lt;strong&gt;KeyBinding&lt;/strong&gt; or &lt;strong&gt;MouseBinding&lt;/strong&gt;.&amp;nbsp; You can
use it on any element which supports input bindings. &lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;There are some helper classes implemented through interfaces
and a (very) simple &lt;strong&gt;ServiceProvider&lt;/strong&gt; to locate them:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=2&gt;&lt;font color=#000000&gt;&lt;strong&gt;IErrorVisualizer &lt;/strong&gt;- to display errors
(title + message).&amp;nbsp; Default implementation displays MessageBox with OK button.&lt;br&gt;
&lt;strong&gt;IMessageVisualer&lt;/strong&gt; - to display messages with a prompt.&amp;nbsp; Default
implementation uses MessgeBox.&lt;br&gt;
&lt;strong&gt;INotificationVisualizer&lt;/strong&gt; - to display a wait prompt.&amp;nbsp; Default
implemation uses an hourglass cursor.&lt;br&gt;
&lt;strong&gt;IUIVisualizer&lt;/strong&gt; - to display other views through a key.&amp;nbsp; Supports
both modal and modaless display.&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;You use the &lt;strong&gt;ServiceProvider&lt;/strong&gt; to find the
services - the base ViewModel class has support built in for it:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a31515&gt;Resolve&amp;lt;&lt;font color=#ff0000&gt;IErrorVisualizer&lt;/font&gt;&amp;gt;().Show&lt;font color=#0000ff&gt;("An
Error Occurred"&lt;/font&gt;, e.Description);&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;You can register the default implementation for all of
the above in the App.xaml.cs file - &lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a31515&gt;ViewModel.RegisterKnownServiceTypes();&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;You can also provide your own implementation through the &lt;strong&gt;ServiceProvider &lt;/strong&gt;itself.&amp;nbsp;
The &lt;strong&gt;ViewModel&lt;/strong&gt; class has a public static field:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a31515&gt;ServiceProvider.Add(&lt;font color=#0000ff&gt;typeof&lt;/font&gt;(&lt;font color=#ff0000&gt;IErrorVisualizer&lt;/font&gt;), &lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#ff0000&gt;MyErrorVisualizer&lt;/font&gt;());&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;This replaces or adds the given service (using the type
as the key) to the registry database.&amp;nbsp; You then use &lt;strong&gt;Resolve&lt;T&gt;
&lt;/strong&gt; to find the service at runtime in any view.&amp;nbsp; Creating secondary views
is done through the &lt;strong&gt;IUIVisualizer&lt;/strong&gt; interface.&amp;nbsp; The default implementation
provides a registry and must be added explicitly to use it:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a52a2a&gt;IUIVisualizer&lt;/font&gt;&amp;nbsp;&lt;font color=#ff0000&gt;controller&lt;/font&gt; = &lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#a52a2a&gt;UIVisualizer&lt;/font&gt;(&lt;font color=#0000ff&gt;new&lt;/font&gt;&lt;STRING, Type&gt; Dictionary&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{Dialogs.AddNewPage, typeof(AddNewPageWindow)},&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{Dialogs.ManagePages, typeof(BrowseWindow)},&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{Dialogs.NewLogon, typeof(LoginDialog)}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
});&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a52a2a&gt;ServiceProvider.Add&lt;/font&gt;(&lt;font color=#0000ff&gt;typeof&lt;/font&gt;(&lt;font color=#ff0000&gt;IUIVisualizer&lt;/font&gt;), &lt;font color=#ff0000&gt;controller&lt;/font&gt;);&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
&lt;font color=#000000 size=2&gt;This adds three UI dialogs to the visualizer - the key
is a simple string, the second parameter is a &lt;strong&gt;Type&lt;/strong&gt; object that represents
the &lt;strong&gt;Window&lt;/strong&gt; to create.&amp;nbsp; You then get the window to display through
the &lt;strong&gt;IUIVisualizer&lt;/strong&gt; interface:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a52a2a&gt;LoginViewData&lt;/font&gt; ld = &lt;font color=#0000ff&gt;new&lt;/font&gt; LoginViewData();&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=4&gt;&lt;font color=#a52a2a&gt;IUIVisualizer&lt;/font&gt;&amp;nbsp;&lt;font color=#ff0000&gt;uiController&lt;/font&gt; = &lt;font color=#a52a2a&gt;ServiceProvider.Resolve&lt;IUIVISUALIZER &gt;
&lt;/font&gt;();&lt;br&gt;
if (&lt;font color=#ff0000&gt;uiController&lt;/font&gt;.ShowDialog(&lt;font color=#ff0000&gt;Dialogs.NewLogon&lt;/font&gt;, &lt;font color=#a52a2a&gt;ld&lt;/font&gt;).&lt;font color=#0000ff&gt;Value &lt;/font&gt;== &lt;font color=#0000ff&gt;true&lt;/font&gt;)&lt;br&gt;
{&lt;br&gt;
}&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;This shows the login dialog, using the &lt;strong&gt;LoginViewData&lt;/strong&gt; as
the &lt;strong&gt;DataContext&lt;/strong&gt; (ViewModel).&amp;nbsp; It displays it as a modal dialog
and returns the final result.&amp;nbsp; You can also display modaless dialogs which take
an optional completion proc:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" color=#000000 size=3&gt;&lt;strong&gt;public bool Show(string key,
object state, bool setOwner, EventHandler&lt;UICOMPLETEDEVENTARGS&gt;
completedProc)
&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;The completed procedure is invoked when/if the window ever
closes.&amp;nbsp; Again, the state passed is the data context and if it derives from ViewModel
the appropriate Closing/Activated events will be wired up to the events in the ViewModel.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;I've been using this library for quite a while and it works
great (for me).&amp;nbsp; I provide it here with full source code so you can diagnose
any issues you have, or just look through it.&amp;nbsp; Feel free to use it however you
like.&amp;nbsp; I don't claim this to be the end-all implementation - as I said much of
the ideas expressed in here&amp;nbsp;can be found&amp;nbsp;elsewhere. &amp;nbsp;There is also
a help file provided and a simple example of how to use some of the classes provided
that I whipped up for fun.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.julmar.com/samples/mvvmhelpers.zip"&gt;Here's the download with everything
packaged.&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;Enjoy - let me know if you have any issues or think of
a good addition!&amp;nbsp; Just remember I put no guarentee on this code - consider it
a sample for you to do whatever you like with.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000 size=2&gt;&lt;/font&gt;&amp;nbsp;
&lt;/p&gt;
&lt;font color=#a52a2a&gt;&lt;/font&gt;&gt;&gt;&lt;img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=8b3e4279-70a5-431e-8fa3-4c1e047df311" /&gt;</content>
  </entry>
  <entry>
    <title>Adding to an ObservableCollection from a background thread</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/04/01/AddingToAnObservableCollectionFromABackgroundThread.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,81cff507-980a-4086-bcd5-ece419ca33d4.aspx</id>
    <published>2009-04-01T10:17:37.33-07:00</published>
    <updated>2009-04-08T10:18:04.7677908-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
One annoying thing about <strong>ObservableCollection&lt;T&gt;</strong> is that it
doesn't support modifications from background threads.  That is to say, the <strong>CollectionChanged</strong> notification
doesn't marshal to the proper thread when it is raised and it causes the exception
"<strong>This type of CollectionView does not support changes to its SourceCollection
from a thread different from the Dispatcher thread</strong>".  
</p>
        <p>
I ran into this problem a while back and searched out to see if someone else had solved
it. I found a solution from Tamir Khason (a fellow WPF disciple), he wrote
a thread-safe version (<a href="http://blogs.microsoft.co.il/blogs/tamir/archive/2007/04/22/Thread-safe-observable-collection.aspx">http://blogs.microsoft.co.il/blogs/tamir/archive/2007/04/22/Thread-safe-observable-collection.aspx</a>),
but I didn't want to add the locking into the collection itself (I want to manage
it at a higher level myself).  Really, all I want is to do the notification on
the proper (Dispatcher) thread.  
</p>
        <p>
It turns out to be pretty easy, here's my solution:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> MTObservableCollection&lt;T&gt;
: ObservableCollection&lt;T&gt;<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">event</span> NotifyCollectionChangedEventHandler
CollectionChanged;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
protected</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> OnCollectionChanged(NotifyCollectionChangedEventArgs
e)<br />
   {<br />
      var eh <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> CollectionChanged;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">     
if</span> (eh !<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>)<br />
      {<br />
         Dispatcher dispatcher <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (from
NotifyCollectionChangedEventHandler nh <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> eh.GetInvocationList()<br />
                
let dpo <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> nh.Target <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">as</span> DispatcherObject<br />
                
where dpo !<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span><br />
                
select dpo.Dispatcher).FirstOrDefault();<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">       
if</span> (dispatcher !<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span> &amp;&amp;
dispatcher.CheckAccess() == <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">false</span>)<br />
        {<br />
           dispatcher.Invoke(DispatcherPriority.DataBind,
(Action)(() =&gt; OnCollectionChanged(e)));<br />
        }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">        else</span><br />
        {<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">          
foreach</span> (NotifyCollectionChangedEventHandler nh <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> eh.GetInvocationList())<br />
              nh.Invoke(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">this</span>,
e);<br />
        }<br />
     }<br />
  }<br /></span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">}<br /></span>
        </p>
        <img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=81cff507-980a-4086-bcd5-ece419ca33d4" />
      </div>
    </content>
  </entry>
  <entry>
    <title>LINQ is just freaking cool</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/03/05/LINQIsJustFreakingCool.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,06efbdab-d87c-43d3-8f85-fbeadb197159.aspx</id>
    <published>2009-03-05T09:27:19.076-08:00</published>
    <updated>2009-03-05T09:29:08.7790956-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
A couple of weeks ago, Jason Whittington (a fellow instructor at <a href="http://www.develop.com">Developmentor</a>) and
I were doing a talk on asynchronous programming and we started with a very simple
example of the APM -- using two loops to create and then consume the IAsyncResult
work:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> TwoLoopMain(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br />
   Queue&lt;IAsyncResult&gt; ars <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Queue&lt;IAsyncResult&gt;();<br />
   Func&lt;int,int,int&gt; mathProc <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Multiply;</span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 1;
i &lt;= 20; i++)<br />
   {<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">    
 for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> j <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 1;
j &lt;= 20; j++)<br />
         ars.Enqueue(mathProc.BeginInvoke(i,
j, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>));<br />
   }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 1;
i &lt;= 20; i++)<br />
   {<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">      for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> j <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 1;
j &lt;= 20; j++)<br />
      {<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">        
int</span> result <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> mathProc.EndInvoke(ars.Dequeue());<br />
         Console.SetCursorPosition(i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">*</span> 3,
j);<br />
         Console.Write(result);<br />
     }<br />
  }<br />
}<br /><br /></span>
        </p>
        <p>
We had already presented a talk on LINQ earlier in the week and so I thought we might
be able to do the above in a single expression with LINQ - kind of a challenge ..
here's what we came up with:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> LinqTest()<br />
{<br />
   Func&lt;int,int,int&gt; mathProc <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Multiply;<br /><br />
   (from i <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> Enumerable.Range(1,
20)<br />
    from j <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> Enumerable.Range(1,
20)<br />
    select <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> {
i, j, ar <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> mathProc.BeginInvoke(i,
j, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>)
})<br /></span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">     
.ToList()<br />
      .ForEach(e =&gt; {<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">       
int</span> result <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> mathProc.EndInvoke(e.ar);<br />
        Console.SetCursorPosition(e.i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">*</span> 3,
e.j);<br />
        Console.Write(result);<br />
     });<br />
}<br /></span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">It's
not easily readable (and so I wouldn't promote this for production code), but it's
way cool and an example of how LINQ (and functional programming in general) is really
changing the way programmers think about code.. just freaking cool..</font>
            <br />
          </span>
        </p>
        <img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=06efbdab-d87c-43d3-8f85-fbeadb197159" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Adding NAS folders to Windows 7 libraries</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/02/24/AddingNASFoldersToWindows7Libraries.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,da6b915b-69ea-4396-beae-cba2315bfce2.aspx</id>
    <published>2009-02-23T18:33:36.254-08:00</published>
    <updated>2009-02-23T18:36:02.5972254-08:00</updated>
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I use a lot of virtual machines - and it's always handy to store files onto the real
machine through shared folders. With Windows 7 and the new library feature I thought
it would be really cool to add the shares to the Documents Library but found that
the underlying system must be running Indexing Server 4.0 -- not an option for my
OSX based system! 
</p>
        <p>
Here's a nice little workaround that lets you add non-indexed folders though: 
</p>
To add a non-indexed UNC as a library to Windows 7 Beta: 
<br /><br />
1. Create a folder on your hard drive for shares. I used <b>c:\users\Mark\Shares\Documents</b>.<br />
2. Add the new folder to your library using Explorer.<br />
3. Delete the folder using a command prompt window.<br />
4. Use the mklink command to create a symbolic link with the Documents name to your
share. In my case the proper command was:<br /><p><b>mklink /D Documents \\.Host\Documents</b></p><p>
Note that mklink.exe requires administrator access so start the command prompt by
holding SHIFT+CTRL and clicking on it (or use the "Run As Administrator" option)
</p><br />
Voila! Explorer keeps the folder in the library even though it's not indexed.<img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=da6b915b-69ea-4396-beae-cba2315bfce2" /></div>
    </content>
  </entry>
  <entry>
    <title>It's been such a long time.. [WPF + Windows 7]</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2009/01/29/ItsBeenSuchALongTimeWPFWindows7.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,eca611aa-f16c-4db9-b84b-5149984d9f3b.aspx</id>
    <published>2009-01-29T10:02:21.328-08:00</published>
    <updated>2009-06-29T09:51:35.6379786-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <category term="Code" label="Code" scheme="http://www.julmar.com/blog/mark/CategoryView,category,Code.aspx" />
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <b>Update</b>
        <br />
Microsoft has released the API code pack including Windows 7 support -- get it here: <a href="http://code.msdn.microsoft.com/WindowsAPICodePack">http://code.msdn.microsoft.com/WindowsAPICodePack</a><br /><br />
Well, it's been a while since I posted anything, I'm sorry! 
<br /><br />
I've been busy working with Windows 7 and Microsoft Surface touch-computing. To that
end, I needed to get access to some of the new Windows 7 APIs in managed code ...
which isn't supported yet (but is coming). 
<br /><br />
So I built an interop library to access: <strong><br /><br /></strong><ul><li><strong>Scenic Ribbon (native Win32 ribbon) in Windows Forms</strong></li><li><strong> Native WM_TOUCH and WM_GESTURE messages</strong></li><li><strong> Sensor API</strong></li><li><strong> Shell APIs (jump lists, thumbnail buttons, libraries) </strong></li></ul><strong>One note:</strong> I make no guarantee that everything is correct (it's hard
to do that on a shifting beta with minimal docs, but in addition I'm not sure I've
gotten 100% coverage with everything anyway). Here's the interop library with source
code: 
<br /><br /><a href="http://www.julmar.com/samples/win7Interop.zip">Windows 7 Beta 1 Interop Library
for .NET 2.0</a><br /><br />
I'll be posting more details and samples a bit later, here's a couple to get you started. 
<br /><br />
Here's a simple example of using the Scenic Ribbon and native touch support to create
a (very) simple Windows Forms finger<br />
paint program:<br /><br /><img src="http://www.julmar.com/blog/mark/content/binary/touch_img.jpg" alt="touch_img.jpg" border="0" height="469" width="699" /><br /><br /><a href="http://www.julmar.com/samples/touch_sample.zip">Multi Touch example with
Windows Forms</a><br /><br />
Here's a simple example of using the gestures and library support in a WPF application.
It grabs all the directories in your Pictures Library and then shows you each picture
and lets you use the swipe gesture to move between then, pinch to scale and of course,
rotate. 
<br /><br /><img src="http://www.julmar.com/blog/mark/content/binary/gesture_img.jpg" alt="gesture_img.jpg" border="0" height="372" width="498" /><br /><br /><a href="http://www.julmar.com/samples/gesture_sample.zip">Gesture example with WPF</a><br /><br />
Both of these samples work with the HP Touchmate (and multi-touch drivers) and Windows
7 Beta 1. Have fun! -mark<img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=eca611aa-f16c-4db9-b84b-5149984d9f3b" /></div>
    </content>
  </entry>
  <entry>
    <title>Using the ImplicitStyleManager with Headered controls</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2008/11/13/UsingTheImplicitStyleManagerWithHeaderedControls.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,2b285ed0-c431-4d9d-b9b9-0948588bb4f7.aspx</id>
    <published>2008-11-13T07:42:01.1036853-08:00</published>
    <updated>2008-11-13T07:42:01.1036853-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been playing with the Silverlight toolkit (released at PDC) this week and ran
across a pretty nasty edge case related to HeaderedContentControl and the implicit
style manager.  If you create something like this, where the Header is set to
a Visual of some kind:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;UserControl
x:Class="SilverlightPrototype.Page"<br />
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
<br />
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
    xmlns:sys="clr-namespace:System;assembly=mscorlib"<br />
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"&gt;<br /><br />
&lt;StackPanel x:Name="LayoutRoot" Background="White"&gt;<br />
   &lt;Button Click="Button_Click" Content="Change Style" /&gt;<br />
   &lt;Controls:HeaderedContentControl&gt;<br /><font color="#0000ff">      &lt;Controls:HeaderedContentControl.Header&gt;<br />
         &lt;TextBlock <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>="Header"
Foreground="DarkBlue" FontWeight="Bold" /&gt;<br />
      &lt;/Controls:HeaderedContentControl.Header&gt;<br /></font>      &lt;TextBlock <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>="Body"
/&gt;<br />
   &lt;/Controls:HeaderedContentControl&gt;<br />
&lt;/StackPanel&gt;<br /></span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;/UserControl&gt;</span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">And
then you change the style of the headered control when the button is clicked:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <font face="Verdana" color="#003300" size="2">
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
                <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Button_Click(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
RoutedEventArgs e)<br />
{            <br />
    Uri uri <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Uri(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">@"SilverlightPrototype;component/theming/simplestyle.xaml"</span>,
UriKind.Relative);<br />
    ImplicitStyleManager.SetResourceDictionaryUri(LayoutRoot, uri);<br />
    ImplicitStyleManager.SetApplyMode(LayoutRoot, ImplicitStylesApplyMode.OneTime);<br />
    ImplicitStyleManager.Apply(LayoutRoot);<br />
}<br /></span>
            </p>
          </font>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">Where
the style is just a simple ContentPresenter or ContentControl for the header
and the body:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <font face="Verdana" color="#003300" size="2">
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;Style
TargetType="Controls:HeaderedContentControl"&gt;<br />
   &lt;Setter Property="Template"&gt;<br />
      &lt;Setter.Value&gt;<br />
        &lt;ControlTemplate TargetType="Controls:HeaderedContentControl"&gt;<br />
           &lt;StackPanel Orientation="Horizontal"&gt;<br />
              &lt;ContentPresenter
Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}"
Margin="10" /&gt;<br />
              &lt;ContentPresenter
Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"
Margin="10" /&gt;<br />
           &lt;/StackPanel&gt;<br />
        &lt;/ControlTemplate&gt;<br />
     &lt;/Setter.Value&gt;<br />
   &lt;/Setter&gt;<br />
&lt;/Style&gt;<br /></span>
            </p>
          </font>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">Silverlight
will crash deep in the <strong>DependencyProperty.SetValue</strong> code with an <strong>ArgumentException</strong> indicating
that the TextBlock for the header is invalid.  The issue appears to be that the
TextBlock defined as the header content is already part of the visual tree and therefore
cannot be bound to the new control template.  Content works fine so there is
some other path being taken for that property.</font>
          </span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">The
workaround is pretty easy, just use a non-visual in the header and then supply a DataTemplate
to give it the appropriate visual tree.  Silverlight will re-create the visual
tree each time from the data template so we don't have the reuse issue:</font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <font face="Verdana" color="#003300" size="2">
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;StackPanel
x:Name="LayoutRoot" Background="White"&gt;<br /><br />
&lt;StackPanel.Resources&gt;<br />
   &lt;!<span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Header works as long as template is supplied - i.e. non-visual element applied directly
to header --&gt;</span><br />
   &lt;DataTemplate x:<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Key</span>="HeaderTemplate"&gt;<br />
      &lt;TextBlock Foreground="DarkBlue" FontWeight="Bold" <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>="{Binding}"
/&gt;<br />
   &lt;/DataTemplate&gt;<br />
&lt;/StackPanel.Resources&gt;<br /><br />
&lt;Button Click="Button_Click" Content="Change Style" /&gt;<br /><br />
&lt;Controls:HeaderedContentControl <strong><font color="#0000ff">HeaderTemplate="{StaticResource
HeaderTemplate}"</font></strong>&gt;<br /><font color="#0000ff">   &lt;Controls:HeaderedContentControl.Header&gt;<br />
      &lt;sys:String&gt;Header <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>&lt;/sys:String&gt;<br />
   &lt;/Controls:HeaderedContentControl.Header&gt;<br /></font><br />
   &lt;TextBlock <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Text</span>="Body"
/&gt;<br />
&lt;/Controls:HeaderedContentControl&gt;<br /><br />
&lt;/StackPanel&gt;<br /></span>
            </p>
          </font>
        </span>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <font face="Verdana" color="#003300" size="2">If
you need more than a string, then just define an object and assign that instead -
for example:</font>
              </span>
            </font>
          </span>
        </p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <font face="Verdana" color="#003300" size="2">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <p>
                <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                  <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> HeaderStuff<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> Text
{ get; set; }<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">  
public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">bool</span> IsChecked
{ get; set; }<br />
}<br /><br />
&lt;DataTemplate x:Key=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"HeaderStuff"</span>&gt;<br />
   &lt;StackPanel&gt;<br />
      &lt;Rectangle Fill=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Blue"</span> Stroke=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Yellow"</span> Width=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"50"</span> ...
/&gt;<br />
      &lt;CheckBox IsChecked=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"{Binding
IsChecked}"</span> Content=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"{Binding
Text}"</span> /&gt;<br />
   &lt;/StackPanel&gt;<br />
&lt;/DataTemplate&gt;<br /><br />
...<br /><br />
&lt;Controls:HeaderedContentControl HeaderTemplate=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"{StaticResource
HeaderStuff}"</span>&gt;<br />
   &lt;Controls:HeaderedContentControl.Header&gt;<br />
      &lt;me:HeaderStuff IsChecked=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"True"</span> Text=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"This
is a checkbox"</span> /&gt;<br />
   &lt;/Controls:HeaderedContentControl.Header&gt;<br />
&lt;/Controls:HeaderedContentControl.Header&gt;<br /></span>
              </p>
            </span>
          </font>
          <p>
            <br />
          </p>
        </span>
        <img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=2b285ed0-c431-4d9d-b9b9-0948588bb4f7" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Debugging WPF with VS2010</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2008/11/12/DebuggingWPFWithVS2010.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,f2689d26-2062-4b7b-a077-4912b976d637.aspx</id>
    <published>2008-11-12T11:06:04.185-08:00</published>
    <updated>2008-11-12T11:15:17.6506206-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.julmar.com/blog/mark/CategoryView,category,NET.aspx" />
    <category term="WPF" label="WPF" scheme="http://www.julmar.com/blog/mark/CategoryView,category,WPF.aspx" />
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">One of the coolest new debugging features
included with VS2010 CTP1 is the management of WPF event traces directly in the IDE. 
With this feature you can turn specific event traces on and off and have them show
up in the debug window.  The default setting is to just show data binding errors
- the existing behavior.  But now you can view resource lookups, routed event
creation, etc. Here's the dialog:<br /><br /><br /><p></p><img src="http://www.julmar.com/blog/mark/content/binary/NewDebugFeatures11.jpg" border="0" /><img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=f2689d26-2062-4b7b-a077-4912b976d637" /></div>
    </content>
  </entry>
  <entry>
    <title>Demos from Silverlight 2 class this week</title>
    <link rel="alternate" type="text/html" href="http://www.julmar.com/blog/mark/2008/10/02/DemosFromSilverlight2ClassThisWeek.aspx" />
    <id>http://www.julmar.com/blog/mark/PermaLink,guid,3c68e467-d5cf-4624-8566-f849026444b6.aspx</id>
    <published>2008-10-02T07:04:48.613355-07:00</published>
    <updated>2008-10-02T07:04:48.613355-07:00</updated>
    <author>
      <name>Mark</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <a href="http://www.julmar.com/DMStuff/sl2_demos.zip">Here
are the demos</a> for the guys who attended the 2-day binding + network Silverlight2
training in Boston.  Thanks guys!<br /><br /><br /><p></p><img width="0" height="0" src="http://www.julmar.com/blog/mark/aggbug.ashx?id=3c68e467-d5cf-4624-8566-f849026444b6" /></div>
    </content>
  </entry>
</feed>