<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>My Team Expression</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/micmcd/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/micmcd/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:25:25 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Silverlight and air - teh 3vil?</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/silverlight-and-air-teh-3vil</link>
      <pubDate>Tue, 19 Aug 2008 09:52:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/08/19/silverlight-and-air-teh-3vil/</guid>
      <description><![CDATA[I've been radio silent for quite some time now -- my apologies, but changing jobs and moving across...]]></description>
      <content:encoded><![CDATA[<P>I've been radio silent for quite some time now -- my apologies, but changing jobs and moving across the country will do that to even the most faithful blogger (which I don't think I was to begin with).&nbsp; I've moved over to Visual Studio Team System, hence the name change of the blog (My Expression&nbsp; =&gt; My Team Expression).&nbsp; The topic of the blog will change slightly, though I hope to keep a focus on WPF and C# tools and tricks, because that is where my passion lies.&nbsp; To that end, I found a couple of interesting discussions about the merits and issues of Silverlight and AIR.&nbsp; <EM>I</EM> think Silverlight comes out in the lead, but my opinions may be biased.&nbsp; </P>
<P><A href="http://www.theregister.co.uk/2008/08/18/silverlight_pros_and_cons/print.html" mce_href="http://www.theregister.co.uk/2008/08/18/silverlight_pros_and_cons/print.html">10 Good Things and 10 Bad Things About Silverlight</A>&nbsp; </P>
<P><A href="http://www.itwriting.com/blog/310-adobe-air-10-reasons-to-love-it-10-reasons-to-hate-it.html" mce_href="http://www.itwriting.com/blog/310-adobe-air-10-reasons-to-love-it-10-reasons-to-hate-it.html">10 Good Things and 10 Bad Things About AIR</A></P>
<P>One omission, in my mind, from the list of bad things about AIR, is the lack of a decent IDE.&nbsp; I've tried to mess around with it... and it seems that most people use Dreamweaver?? (I could be wrong on this -- would appreciate correction in comments if so)&nbsp; </P>
<P>Dreamweaver is nice enough for webby things, but it lacks the single most important feature of a grown-up IDE: Intellisense!!! </P>
<P>I may lose mad 1337 points for so saying, but I find the act of programming without code-completion to be almost barbaric.&nbsp; Code flows much more quickly when you have properly working Intellisense.&nbsp; I say "properly working" because I used to use Eclipse 3.1, which would routinely freeze when it tried to bring up the code completion menu after an object with a lot of methods or fields in Java.&nbsp; VS rocks out w.r.t. fast, powerful code completion.&nbsp; I've seen others do it well, but not well enough.&nbsp; I'm interested to try the Komodo IDE for Ruby -- haven't gotten there yet <STRIKE>b/c it costs a couple hundred bucks that I haven't got to spare right this second</STRIKE>.&nbsp; I stand corrected -- I'm now told the free version of Komodo does in fact have code-completion.&nbsp; Looks like I have a new tool to mess around with tonight.&nbsp; I'll have to blog about it later -- I'm currently trying to learn a lot of Ruby so I can make a cool SL - IronRuby project, though I have no idea what that project should be.</P>
<P>To me, code completion is the best way to pick up and run with an API or language with which you aren't intimately familiar.&nbsp; There's no shame in that -- a good programmer should be able to sit down and hack away at a completely foreign language with some success, provided the API is well-written.&nbsp; As you get more familiar with the language, CC becomes a way to save a few keystrokes.&nbsp; Good code (at least nowadays) often has very long method and object names, theoretically to make it more descriptive and hence easy to use.&nbsp; I routinely type "myObject.xxx...", adding the first few letters of my method name, then strike the down key and enter, saving me the rest of the method name.&nbsp; My productivity is increased, and the waste of what I consider "stupid memory"** is avoided.&nbsp; </P>
<P>Sometimes the IDE can make the language.&nbsp; Maybe I'm just not old-fashioned enough, but that's another story.&nbsp; I'm a fan of Silverlight, but that's to be expected from someone who used to work on Expression Blend.&nbsp; The Olympics coverage online is great.&nbsp; </P>
<P>My only complaint with the Olympics coverage is not with Silverlight, but with the NBC layout.&nbsp; Why not have all recorded events laid out simply in a list, organized by event?&nbsp; I want to watch the soccer games, including some of the older matches that I missed (if I can avoid seeing the score), but it is a pain to figure out where to go to get the video to come up.&nbsp; Some of the older matches aren't even available.&nbsp; I know demand might be low for those games, but it doesn't seem to me that the cost of making them easily available would be high.&nbsp; They do have some old games up, but not all.</P>
<P>I also found an interesting speculation on whether or not Silverlight was "evil," very much related to the Olympics coverage, <A href="http://www.theregister.co.uk/2008/08/12/evil_silverlight/" mce_href="http://www.theregister.co.uk/2008/08/12/evil_silverlight/">here</A>.</P>
<P>Anyhow -- enough ranting.&nbsp; Micmcd out.</P>
<P>** Stupid memory: Waste of human memory on things that are more easily automated and don't cost the human much not to know, provided they are using a modern system.&nbsp; <EM>Perfect</EM> API memorization is one of those, IMHO.&nbsp; A programmer should have an idea of what objects can and cannot do, but memorizing the exact spelling of the methods?&nbsp; It's a waste of perfectly good brainspace.</P>]]></content:encoded>
    </item>
    <item>
      <title>Avoiding a WPF memory leak with DataBinding (Black Magic)</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/avoiding-a-wpf-memory-leak-with-databinding-black-magic</link>
      <pubDate>Fri, 07 Mar 2008 19:01:42 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/03/07/avoiding-a-wpf-memory-leak-with-databinding-black-magic/</guid>
      <description><![CDATA[A quick note that can save you a lot of pain later: I mentioned a while back that binding to an...]]></description>
      <content:encoded><![CDATA[<p>A quick note that can save you a lot of pain later: I mentioned a while back that binding to an ObservableCollection&lt;T&gt; is preferable to binding to a List&lt;T&gt;.&#160; The reason was that changes to an ObservableCollection are immediately handled by the object to which it is bound, unlike a list.&#160; Behind the scenes, the reason for this is that ObservableCollection implements the INotifyPropertyChanged interface.&#160; Ordinarily I'd be quite happy to let the black magic do its work, but there is a huge pain point related to INotifyPropertyChanged.</p>  <p>WPF has a sadly unavoidable memory leak related to data binding, and the problem is greatly exacerbated by Blend.&#160; For specifics, you can look at <a href="http://blogs.msdn.com/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx">http://blogs.msdn.com/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx</a> .&#160; The leak isn't an awful oversight by the WPF team -- it is caused when databinding to any CLR object, and most data sources stick around for the lifetime of the app, so it isn't regarded as a horrible thing to hold onto (the memory is released when the app is closed).&#160; In Blend, however, if you don't play by very specific rules you can leak the entire visual tree every time you make a change, such as when you go to edit a template, build, make an animation, and many other cases.&#160; In each instance, Blend is making WPF's quasi-mistake over and over.&#160; With a decently sized project, it is trivial to get Blend to be using upwards of 1GB(!!!) of memory simply by making repeated edits to a template.&#160; Blend rebuilds data sources quite frequently when making certain types of edits.&#160; I stumbled upon this issue quite accidentally.</p>  <p>How to fall into the trap:</p>  <p>You have a ListBox in your project bound to an ObservableCollection&lt;MyClass&gt;.&#160; You're quite happy with yourself for using an OC rather than a List, and now whenever you add another instance of MyClass to the OC, you see it in your listbox.&#160; You have a TextBox, however, whose data context is bound to the selected item of the listbox.&#160; The textBox's text is bound to the Foo field of the Data Context, which is a MyClass.&#160; You wouldn't ordinarily think that you'd need to have MyClass know anything about databinding, or to implement any wacky interfaces or anything of the sort.&#160; Maybe the Foo field can't even be changed, only displayed -- so why bother implementing INotifyProperyChanged?&#160; </p>  <p>What happened:</p>  <p>There is an issue where WPF checks to find things that implement INotifyProperyChanged.&#160; If there is a databinding to something not implementing this interface, then it makes a record in a global table.&#160; That record doesn't get cleaned up, as WPF has no way of checking when that DB record is no longer needed.&#160; Everything could work perfectly in your app when you run it, but if you make certain kinds of edits in Blend on the project you can leak memory like... hmmm... like a wet diary?&#160; Did that simile work?&#160; Anyhow...</p>  <p>To Fix:</p>  <p>Anything involved in a databinding should implement INotifyPropertyChanged.&#160; You don't have to raise the PropertyChanged event if you don't particularly feel like it, but to avoid the leak you have to implement the interface.&#160; The annoying side effect here is that it can lead to lots of squiggly green lines in Visual Studio nagging you about declaring an event you haven't done anything with (you have to declare the PropertyChanged event to implement the interface), but this doesn't hurt your code (other than aesthetically).&#160; Add a few lines of useless code, and you won't have to deal with an incredibly sluggish Blend.</p>  <p>Note that, in order to avoid the memory leak you just have to implement the interface.&#160; If you actually want to use it, you need to make calls to NotifyProperyChanged as I mentioned a while back (<a title="http://blogs.msdn.com/micmcd/archive/2008/01/22/inotifychanged-magic-also-of-the-black-arts.aspx" href="http://blogs.msdn.com/micmcd/archive/2008/01/22/inotifychanged-magic-also-of-the-black-arts.aspx">http://blogs.msdn.com/micmcd/archive/2008/01/22/inotifychanged-magic-also-of-the-black-arts.aspx</a>).&#160; </p>]]></content:encoded>
    </item>
    <item>
      <title>Quick Gotcha with SelectionChanged on ComboBoxes</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/quick-gotcha-with-selectionchanged-on-comboboxes</link>
      <pubDate>Thu, 28 Feb 2008 14:23:53 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/02/28/quick-gotcha-with-selectionchanged-on-comboboxes/</guid>
      <description><![CDATA[Been a while since I've been working on the soon-to-be-not-so-uber-secret private project, but now...]]></description>
      <content:encoded><![CDATA[<p>Been a while since I've been working on the soon-to-be-not-so-uber-secret private project, but now that I'm back on task I've come across a nice little coding gotcha to share with my loyal reader(s).</p>  <p>Situation: You have a ComboBox on your application that changes some major setting, and you'd like to pop up a MessageBox to the user to let her know she's doing something big and to verify the action whenever she changes the SelectedItem of the ComboBox.&#160; In fact, if she decides against it, you undo the change.</p>  <p>(Bad) Solution:</p>  <p>EssentialDataGrouping edg = (EssentialDataGrouping)this.DataContext;    <br />Universe old = edg.CurrentUniverse;     <br />Universe temp = this.UniverseComboBox.SelectedItem as Universe;     <br />MessageBoxResult result = MessageBox.Show(&quot;Change current universe?&#160; Changes will be saved to this universe.&quot;, temp.ToString(), MessageBoxButton.OKCancel);     <br />switch (result)     <br />{     <br />&#160;&#160;&#160; case MessageBoxResult.OK:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; edg.SaveUniverse();     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; edg.CurrentUniverse = temp;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.currentUniverse = temp.ID;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;     <br />&#160;&#160;&#160; case MessageBoxResult.Cancel:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.changingUnivese = false;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.UniverseComboBox.SelectedItem = old;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;     <br />&#160;&#160;&#160; default:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;     <br />}</p>  <p>Problem: The SelectionChanged even fires when you first load the window, so the user gets that confirmation box before the app starts.&#160; Also, if she chooses to cancel her change, the event gets fired again, since you are actually changing the SelectedItem...</p>  <p>(Better) Solution:</p>  <p>Your warning code needs to be put in a separate private method, whose input is a bool that basically decides whether or not the logic should be run.&#160; What you really want to do is run this logic whenever the user is trying to change the selection of the combobox, and now run the logic when you change the selection behind the scenes or when the event is fired by setup.&#160; </p>  <p>This can be done by creating a global bool variable that is set to false by default.&#160; The only way the user should be changing the universe is with a mouse click.... so why not handle those events? (Note: if you want the user to be able to use the keyboard as well, you can turn on the global flag when the ComboBox has Keyboard focus and off when it loses focus.)&#160; PreviewMouseLeftButtonDown and MouseLeftButtonUp are the pair you want... set the global to true on PreviewMouseLeftButtonDown and false on MouseLeftButtonUp.&#160; You want PreviewMLBDown rather than MLBDown b/c you want to make sure to turn on the flag before the click.</p>  <p>In the case where you change the selection yourself (when the user chooses to cancel her action), you need only set the global bool to false.&#160; Note that at the end of the method, if you are allowing for keyboard changes, you should check for keyboard focus and change the global flag to true if focused (otherwise you'd cancel and then not check again if the user doesn't shift keyboard focus).</p>]]></content:encoded>
    </item>
    <item>
      <title>How to hack an easy Drag and Drop with Blend</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/how-to-hack-an-easy-drag-and-drop-with-blend</link>
      <pubDate>Thu, 31 Jan 2008 18:43:16 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/31/how-to-hack-an-easy-drag-and-drop-with-blend/</guid>
      <description><![CDATA[WPF is a pretty amazing thing.&#160; Using it, you can take a lot of fairly complicated graphical...]]></description>
      <content:encoded><![CDATA[<p>WPF is a pretty amazing thing.&#160; Using it, you can take a lot of fairly complicated graphical effects, and (esp. using Blend) implement them pretty trivially and make an application look cool.&#160; I have to say, after having banged my head up against the issue for a while, that I'm somewhat disappointed that implementing a feature so basic that people are mad when they can't do it - Drag and Drop, is not so trivial.&#160; I wanted to do something fairly simple in my application: allow the user to grab a handful of items from one listbox in my app and drag them to another.&#160; I've never done this before, but I assumed that it couldn't be too bad - it is, after all, such a basic idea.&#160; Life, however, was not to be quite so simple.&#160; </p>  <p>As it has long been my goal in this blog to spell out the details of anything I find confusing or difficult with WPF programming, Blend, or in general any MS tool I find myself using, I have here laid out the steps to create a sample drag&amp;drop implementation using Blend and VS.&#160; It was more painful than I think it ought to be, but it can be made simple if you know what you are doing.&#160; </p>  <p>The application is simple; a window with two listboxes.&#160; Both are databound to different ObservableCollections (see my earlier post on why you should be binding to these instead of Lists) of SampleDataObjects.&#160; I didn't want to bind to lists of strings, even though in this project a SampleDataObject is a dummy class with one field, &quot;Name,&quot; that is a string... because I wanted this to be easy to morph into something dragging pictures, files, chickens... you name it.</p>  <p>The key points of confusion, at least to me, come from the fact that MouseDown is not the way to go in a listbox -- clicking an item <em>doesn't</em> fire the MouseDown event... I don't know why.&#160; However, clicking in an area of the listbox that is uninhabited does, and gives you plenty of easy ways to crash your app with NullReferenceExceptions.</p>  <p>My solution... or at least the one that works for me and doesn't crash, may be a bit unconventional.&#160; I find... that using PreviewMouseDown and SelectionChanged <em>at the same time</em> works quite marvelously.&#160; Even more comically, if you allow MouseDown to do your original job, but check for nulls, it makes the behavior better.&#160; Hooray for Hacks.</p>  <p>Once again - this is NOT the greatest coding practice in the world.&#160; Just a hack that worked for me.&#160; I spent a little bit of time talking to the people who implemented D&amp;D functionality in Blend, and though there is a much better way to do this from the perspective of writing good code.... for an app that doesn't need a lot of power, it's an awful lot of mess.</p>  <p>Here comes the <strike>science</strike> code:</p>  <p>Start with a pretty simple app.&#160; Two listboxes, two labels.&#160; Gray background on the window is for your eyes' benefit only:</p>  <p><img height="269" alt="Two listboxes.  Nothing fancy." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/startingtwolistboxes.jpg" width="431" /> </p>  <p>Best. App. Ever.</p>  <p>In order to make the listboxes work the way most of us are used to, we need to set the selection mode of both list boxes.&#160; I'm used to being able to select multiple things by holding CTRL whilst I click, but not if I don't.&#160; This is the &quot;Extended&quot; mode, not the &quot;Multiple&quot; mode.&#160; You can set the SelectionMode of a listbox in the Miscellaneous box in the property inspector.</p>  <p><img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/selectionmode.jpg" /> </p>  <p>Moving along, we need to throw some code-behind in here, so I'm going to create another class, which I've called MasterDataHolder.&#160; I've found it to be good practice to create separate data structure (DS) classes and never instantiate them if you intend to use WPF databinding (a.k.a. black magic).&#160; </p>  <p>My DS Class, in its entirety:</p>  <p>------------------------------------------------------</p>  <p>public class MasterDataHolder   <br />{ </p>  <p>&#160;&#160;&#160; private ObservableCollection&lt;SampleDataObject&gt; firstCollection;   <br />&#160;&#160;&#160; private ObservableCollection&lt;SampleDataObject&gt; secondCollection; </p>  <p>&#160;&#160;&#160; public MasterDataHolder()   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.InitializeObjects(); </p>  <p>&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; private void InitializeObjects()   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; string[] numbers = { &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;, &quot;ten&quot; };    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.firstCollection = new ObservableCollection&lt;SampleDataObject&gt;();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.secondCollection = new ObservableCollection&lt;SampleDataObject&gt;();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i = 0; i &lt; numbers.Length; i++)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (i &lt;= numbers.Length / 2)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; firstCollection.Add(new SampleDataObject(numbers[i]));    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; secondCollection.Add(new SampleDataObject(numbers[i]));    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public ObservableCollection&lt;SampleDataObject&gt; FirstCollection   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return this.firstCollection; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; set { this.secondCollection = value; }    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public ObservableCollection&lt;SampleDataObject&gt; SecondCollection   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return this.secondCollection; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; set { this.secondCollection = value; }    <br />&#160;&#160;&#160; }    <br />}</p>  <p>------------------------------------------------------</p>  <p>Recall that my data objects here are of type &quot;SampleDataObject,&quot; a class that has exactly one publicly accessible attribute, a string called &quot;Name.&quot;&#160; Just wanted to get a little more abstract than a list of strings.</p>  <p>To allow the window to have access to the ObservableCollections, I need to instantiate the DataSource.&#160; I do this in Blend with the &quot;+ CLR Object&quot; button at the bottom right, under the properties palette: </p>  <p><img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/addclrobjectbutton.jpg" /> </p>  <p>Go ahead and pick out the MasterDataHolder from the dialog that pops up:</p>  <p><img height="364" src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/addclrobjectdialog.jpg" width="471" /> </p>  <p>Now be sure to set the DataContext property of the Window to the MasterDataHolder you just created:</p>  <p><img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/settingthedatacontextofwindow.jpg" /> </p>  <p>You can then expand the MasterDataHolder in the data pane (in the projects tab on the right side of Blend) and grab the FirstCollection and SecondCollection objects and simply DROP them into the listboxes (++ sweetness... sometimes black magic is awesome).&#160; Remember to pick that you want them to be displayed by their names when the dialog appears after you drop the object into the listbox.</p>  <p>&#160;<img height="382" src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/dragdropdatacontextmenu.jpg" width="467" /></p>  <p>&#160;</p>  <p>You want to have the itemssource bound to an ExplicitDataContext (the third tab option when you are databinding), and chose the Name attribute of the appropriate ObservableCollection.</p>  <p>One last thing you need to make things happen is to set the &quot;AllowsDrop&quot; property of the second listbox.&#160; (Note: in my sample app, you can only drag and drop one way.&#160; It's trivial to reverse it to allow two-way dragging).</p>  <p>&#160;<img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/allowdropcheckbox.jpg" /> </p>  <p>The simplest way (to me) to drag back and forth arbitrary data is to create a custom mover, which I have done with this DataMover.cs class:</p>  <p>----------------------------------------------------------------</p>  <p>public class DataMover   <br />{    <br />&#160;&#160;&#160; private List&lt;SampleDataObject&gt; movingData;    <br />&#160;&#160;&#160; private bool oneToTwo = false; </p>  <p>&#160;&#160;&#160; public DataMover()   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.movingData = new List&lt;SampleDataObject&gt;();    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public void SetData(params SampleDataObject[] sdos)   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public List&lt;SampleDataObject&gt; MovingData   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return this.movingData; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; set { this.movingData = value; }    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public void AddItem(SampleDataObject sam)   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.movingData.Add(sam);    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public bool OneToTwo   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return this.oneToTwo; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; set { this.oneToTwo = value; }    <br />&#160;&#160;&#160; } </p>  <p>}</p>  <p>----------------------------------------------------------------</p>  <p>It's nothing more than a container for some data that I'm going to dump in.&#160; The advantage to using this approach is that you can put complicated logic in there if needs be.&#160; Not sure if that's bad coding practice or not, but then again, this whole technique (from here on out), is bad coding practice, so who cares?&#160; Note that the OneToTwo bool is really only useful if you plan on dragging both ways.&#160; It's an easy field to check to make sure you don't try to implement a drop when the user was aborting his drag before getting out of the starting box.</p>  <p>In order to pull off a drag &amp; drop operation (henceforth DD) in C#/WPF, you need to call the DragDrop.DoDragDrop method.&#160; Check out MSDN for details; this implementation is rather simple.</p>  <p>The MouseDown event is a logical candidate for a place to put the code, so I added the following to my Window1 class:</p>  <p>------------------------------------------------------</p>  <p>private void ListBoxOne_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>  <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dataMover = new DataMover();   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (ListBoxOne.SelectedItems.Count &gt; 0)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </p>  <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach (object ob in this.ListBoxOne.SelectedItems)   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dataMover.AddItem(ob as SampleDataObject);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dragging = true;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>  <p>----------------------------------------------------</p>  <p>Not checking for selection here is an easy way to crash the app.&#160; Anyhow, you also need to implement the MouseMove handler.</p>  <p>------------------------------------------------------</p>  <p>private void ListBoxOne_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)   <br />{    <br />&#160;&#160;&#160; if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed &amp;&amp; this.dataMover.MovingData.Count &gt; 0 &amp;&amp; this.dragging)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; DragDrop.DoDragDrop(this.ListBoxOne, this.dataMover, DragDropEffects.Move);    <br />&#160;&#160;&#160; }    <br />}</p> -------------------------------------------------------  <p>Now you also need to implement the Drop event handler for the target listbox:</p>  <p>-------------------------------------------------------</p>  <p>private void ListBoxTwo_Drop(object sender, DragEventArgs e)   <br />{    <br />&#160;&#160;&#160; try    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; e.Data.GetDataPresent(DataFormats.Serializable);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach (SampleDataObject sam in this.dataMover.MovingData)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (dataMover.OneToTwo)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dataHolder.FirstCollection.Remove(sam);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dataHolder.SecondCollection.Add(sam);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dataMover.OneToTwo = false;    <br />&#160;&#160;&#160; }    <br />&#160;&#160;&#160; catch (Exception ex)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; MessageBox.Show(&quot;no dice: &quot; + e.Data.ToString());    <br />&#160;&#160;&#160; } </p>  <p>}</p>  <p>-----------------------------------------------------</p>  <p>This seems pretty nice.... except that it doesn't work.&#160; Why?&#160; The issue here is that the first listbox doesn't get the MouseDown event.&#160; That event is taken by the selected item, and the listbox itself is none the wiser.</p>  <p>You could implement the exact same code (from MouseDown above) in PreviewMouseDown, which is given to the ListBox, rather than the item.&#160; The issue here is that PreviewMouseDown happens just before the user clicks, so the drag would only grab the items that were selected prior to the click. </p>  <p>Seeing the failings of the PreviewMouseDown event, I was inspired to try the same code in the SelectionChanged event.&#160; This proved <em>almost</em> brilliant.&#160; This failed in the case where you wanted to click and drag the item that was already selected.&#160; Also... if there is only one item in ListBox_One, then you <em>can't</em> change the selection.&#160; That item is stuck.</p>  <p>Here was where the real H4XR brilliance kicked in.&#160; SelectionChanged fails when you want to use the click, but not change the selection (event doesn't fire).&#160; PreviewMouseDown fails precisely when you want to change the selection, but works when you don't.&#160; Hmmm.... SelectionChanged can only be fired <em>after</em> the selection changes, which happens <em>after</em> the click.&#160; PreviewMouseDown happens <em>before</em> the click.&#160; Thus, if both events fired, the SelectionChanged handler would override PreviewMouseDown.&#160; PreviewMouseDown would only be carried out in the cases where SelectionChanged doesn't fire... and those cases just happen to be the ones where PreviewMouseDown works perfectly!&#160; Brilliant!</p>  <p>Only downside to using this combo.&#160; If you click in the empty area below the listbox items and then drag, you still drag over all the selected items (PreviewMouseDown still occurs).&#160; This isn't the end of the world -- there are worse user behaviors and it's an odd thing to do specifically expecting nothing to happen.&#160; You can make the right thing -- nothing -- happen, simply by having the MouseDown handler re-instantiate this.dataMover, clearing anything that PreviewMouseDown gave it (the selection doesn't change, so you don't have to fight that handler).</p>  <p>In summary, both PreviewMouseDown and SelectionChanged on the starting ListBox should read:</p>  <blockquote>   <p>&#160;&#160;&#160;&#160; this.dataMover = new DataMover();     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (ListBoxOne.SelectedItems.Count &gt; 0)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </p> </blockquote>  <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach (object ob in this.ListBoxOne.SelectedItems)   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dataMover.AddItem(ob as SampleDataObject);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.dragging = true;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160; <br /></p>  <p>MouseDown should just read:</p>  <blockquote>   <p>this.dataMover = new DataMover();</p> </blockquote>  <p>And the Drop handler should read as above.&#160; Gorgeous.<img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_24/happyeasydatabindingresults.jpg" /></p>  <p>If this code makes you cringe... well, there's nothing wrong with you.&#160; It probably should do something of the sort.&#160; However, it works, and for a low-power app this might be all you need.</p>  <p>What this can do:</p>  <p>Drag multiple items from one listbox to another <em>in the same window</em>, or at least in the same application.</p>  <p>What this cannot do:</p>  <p>- Drag an item to a specific location in the other listbox - things always show up at the bottom of the list.</p>  <p>- Rearrange items within the same listbox - pretty common functionality, but not universal, and this isn't in any way confusing.</p>  <p>- Drag items out of the application.&#160; I don't really use the parameters of DoDragDrop.</p>  <p>&#160;</p>  <p>Anyhow, if this doesn't offend your sense of decency, it's a neat trick.&#160; You won't find that in the API.&#160; I suspect I might get a pay cut just for having written it, but some things just need to be told.</p>  <p>Incidentally, I promise this code has never, ever, to my knowledge been used in an actual MS product.&#160; I'm fairly certain it never will.&#160; Even I'd buy a Mac if it did... (unless we used it in Mac Office just for kicks).&#160; Enjoy.&#160; </p>  <p>Oh, and go Jints.&#160; Gotta hate the Pats on Sunday.</p>]]></content:encoded>
    </item>
    <item>
      <title>INotifyPropertyChanged Magic (also of the Black Arts)</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/inotifypropertychanged-magic-also-of-the-black-arts</link>
      <pubDate>Tue, 22 Jan 2008 20:34:43 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/22/inotifypropertychanged-magic-also-of-the-black-arts/</guid>
      <description><![CDATA[I've spent the better part of my last four posts talking about the great black magic known as WPF...]]></description>
      <content:encoded><![CDATA[<p>I've spent the better part of my last four posts talking about the great black magic known as WPF Databinding.&#160; As I am continuing my foray into those dark arts, yet another wizard's tool (and the need for it) has come to my attention.&#160; Again... thanks to DanteG... (I do find out things on my own sometimes).</p>  <p>I mentioned in an earlier post that a ListBox's ItemsSource should almost invariably be bound to an ObservableCollection&lt;&gt;, rather than a List&lt;&gt;, because ObservableCollection does some magic behind the scene.&#160; Specifically it notifies everybody bound to it that it has changed.&#160; Having found this, reported it here, and implemented it in my app, I merrily went on my way, assuming that my conversion to the ObservableCollection kingdom would solve all of my updating data vs. data binding properties.</p>  <p>Shockingly, this was not the case.&#160; I had an ObservableCollection of CLR objects I call Actors.&#160; An Actor has a Name property.&#160; My listbox was bound to the ObsCol of Actors, and had a data template that instructed it to display the Name of the Actor.&#160; In the course of mucking about with my app, the user is perfectly capable of changing the name of an Actor.&#160; In fact, that's kind of the point of my app.&#160; Thus, I was not happy when, after going through quite a bit of work to get the names to update properly, given appropriate mucking with the right things, that the ListBox display did not update.&#160; </p>  <p>Enter INotifyPropertyChanged.&#160; If you have an ObsCol of CLR objects to which a listbox is down, the fact that the objects are in an ObsCol ONLY means that adding and removing those CLR objects, Actors in my case, are automatically and instantaneously updated in the listbox.&#160; If you display, for instance, the Name field of the Actor, and you then change the name, you are changing something that isn't currently watched very closely.&#160; Going through sufficient mess can in fact change it, but you need to find a reason for the listbox to completely regenerate its data... and this is just a pain.</p>  <p>The solution is to have your class implement the INotifyPropertyChanged interface.&#160; In VS, a simple right click on the word &quot;INotifyPropertyChanged&quot; will give you an option to implement the interface.</p>  <p>What might be slightly confusing to those who aren't Delegate pro's (a perfectly acceptable situation for those who, like myself, were raised on Java rather than C++), is that you don't have to do anything to the code that comes up as follows:</p>  <p>#region INotifyPropertyChanged Members </p>  <p>public event PropertyChangedEventHandler PropertyChanged; </p>  <p>#endregion</p>  <p>All you need to do is create a method that reads:</p>  <p>private void NotifyPropertyChanged(String info)    <br />{     <br />&#160;&#160;&#160; if (PropertyChanged != null)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; PropertyChanged(this, new PropertyChangedEventArgs(info));     <br />&#160;&#160;&#160; }     <br />}</p>  <p>Then, whenever you change your &quot;Name&quot; attribute, you need to make a call to</p>  <p>NotifyPropertyChanged(&quot;Name&quot;);</p>  <p>You can put in any field name for &quot;Name&quot; to notify of it's change.&#160; The Argument to NotifyPropertyChanged should always be a string, and in fact it should be the actual name (As in the text you typed in) of the method you want to call.&#160; Sorry to beat in the &quot;Yeah, you really type the actual method/field accessor name&quot; bit in, but that actually took me a while to believe was anything more than voodoo.</p>  <p>Just another reason I consider all WPF databinding to be the blackest of magics.</p>]]></content:encoded>
    </item>
    <item>
      <title>The Variable Container</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/the-variable-container</link>
      <pubDate>Wed, 16 Jan 2008 19:59:49 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/16/the-variable-container/</guid>
      <description><![CDATA[The situation -- you have a listbox that you'd like to populate with a bunch of fairly similar, but...]]></description>
      <content:encoded><![CDATA[<p>The situation -- you have a listbox that you'd like to populate with a bunch of fairly similar, but not quite the same, items.&#160; Some of the data is contained in a little textbox, some in a larger one, some in a combobox, or perhaps some things are just checkboxes.&#160; You'd prefer not to create any part of the control in code, as you'd like it to look nice (it can be done, but I'm always more confident of how something looks when I design it in Blend).&#160; </p>  <p>One nice way to get this to work (with suggestions from Dante - <a href="http://blogs.msdn.com/danteg">http://blogs.msdn.com/danteg</a>) is to play games with the Visibility of an object.&#160; Create a new UserControl and make its LayoutRoot a StackPanel with a horizontal Orientation.&#160; Add a label as the first item in the StackPanel.&#160; Then add <em>all</em> of the types of containers you want - textboxes of various sizes, comboboxes, even image containers if you are so inclined.&#160; </p>  <p>Going into the code-behind file, add an enum and a new constructor that takes an argument of that very same type of enum.&#160; Be sure not to delete the default constructor or Blend won't be able to instantiate the object (unless you don't care if Blend is ever able to instantiate it).&#160; This VariableDataContainer, as I have so cleverly labeled mine, is an item added to a ListBox.&#160; When I create the containers, I do so and tell the container what kind of data it will be holding via the enum in the constructor.&#160; The constructor then sets the visibilities of all the inappropriate containers to Visibility.Collapsed, and the right one to Visibility.Visible (this is an Avalon enum).&#160; Ta-da... it now looks like your ListBox is containing various different kinds of objects.</p>  <p>Of course this has the downside of instantiating unnecessary textboxes, comboboxes, and the like, but if you keep the number of containers to a minimum this shouldn't be a horrendous problem.&#160; If your app lags because you are making 5n objects instead of n... your app is a rare bird indeed.&#160; Or just a really powerful one.&#160; Anyhow, my application is not keeping data on every resident of New York, so it works just fine.&#160; Well, the data container works just fine; the app is still under development.</p>  <p>Update: 3/9/08 -- a reader requested the source code for this container, and I have posted a link to it below.&#160; Please bear in mind that the code is not up to production level yet (though it isn't for a Microsoft product anyway), and there are certainly areas that could stand to be improved.</p>  <p><a href="http://www.michaelmcduffee.com/picfolder/msdn/VariableDataContainerCode.txt">VariableContainer.cs</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Dealing with Black Magic and Instantiated Data Sources</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/dealing-with-black-magic-and-instantiated-data-sources</link>
      <pubDate>Fri, 11 Jan 2008 17:25:10 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/11/dealing-with-black-magic-and-instantiated-data-sources/</guid>
      <description><![CDATA[Yet another gotcha in the realm of data binding.&#160; It is such a powerful feature, and yet there...]]></description>
      <content:encoded><![CDATA[<p>Yet another gotcha in the realm of data binding.&#160; It is such a powerful feature, and yet there are a lot of things that can go wrong.&#160; I even showed you part of it in my previous post and didn't realize it till I got bit in my own project.</p>  <p>Let's start out with something pretty simple.</p>  <p><img height="342" alt="Easy as they come, right?" src="http://www.michaelmcduffee.com/picfolder/msdn/post1_11/baseShot.jpg" width="435" />&#160;</p>  <p>Code Behind (only one file):</p>  <p>public partial class Window1   <br />{    <br />&#160;&#160;&#160; private SourceFoo mySource; </p>  <p>&#160;&#160;&#160; public Window1()   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.InitializeComponent();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; mySource = new SourceFoo();    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; private void Button_Click(object sender, RoutedEventArgs e)   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.mySource.Things.Add(&quot;green&quot;);    <br />&#160;&#160;&#160; }    <br />} </p>  <p>public class SourceFoo   <br />{    <br />&#160;&#160;&#160; public ObservableCollection&lt;string&gt; things;    <br />&#160;&#160;&#160; public SourceFoo()    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; things = new ObservableCollection&lt;string&gt;();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; things.Add(&quot;red&quot;);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; things.Add(&quot;yellow&quot;);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; things.Add(&quot;blue&quot;);    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public ObservableCollection&lt;string&gt; Things   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return this.things; }    <br />&#160;&#160;&#160; }    <br />}</p>  <p>--------------</p>  <p>I have the itemsSource of the ComboBox bound to SourceFoo's &quot;Things&quot; field.</p>  <p><img alt="Add the CLR source and bind away." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_11/databindingdanger.jpg" /> </p>  <p>Note here that I had to &quot;Add CLR Object&quot; to put the data source.&#160; Note that in my code, I have instantiated the SourceFoo object, and a Button_Click should add the word &quot;green&quot; to the list.</p>  <p>Go ahead and give it a shot if you like... no matter how many times you push the button, the only colors you'll be able to chose from are red, yellow, and blue.&#160; No green for you.&#160; </p>  <p>Instantiating the Data Source class in code is precisely the wrong thing to do, as I found out the hard way in a much larger application.&#160; When you add a CLR object data source, the object is <em>automatically instantiated for you</em> in a code-behind file you can't change (the g.cs files).&#160; When I gave the command &quot;this.mySource.Things.Add(&quot;green&quot;),&quot; green was added to <em>an</em> instance of the SourceFoo class, but not the one to which my ComboBox was bound.&#160; </p>  <p>A quick workaround to this is to make a data binding on the button (or the window... works just as well), and bind to the DataContext of the button.&#160; This hooks you up with the exact same object.</p>  <p><img alt="Be sure to grab the same DS you used for the other object." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_11/buttondata.jpg" /> </p>  <p>You can then change what happens in the code-behind file.</p>  <p>public Window1()   <br />{    <br />&#160;&#160;&#160; this.InitializeComponent();    <br />} </p>  <p>private void Button_Click(object sender, RoutedEventArgs e)   <br />{    <br />&#160;&#160;&#160; ObservableCollection&lt;string&gt; fooList = (ObservableCollection&lt;string&gt;)this.AdderButton.DataContext;    <br />&#160;&#160;&#160; fooList.Add(&quot;green&quot;);    <br />}</p>  <p>&#160;</p>  <p>My Window1 class just got a lot shorter.&#160; You always have to cast the DataContext of an item down from object, but once there you have the right SourceFoo to deal with.</p>  <p><img alt="See, it does work." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_11/capture.jpg" /> </p>  <p>Remember to do this whenever you want to interact directly with a data source class.&#160; In my case, I had a source CLR class that was acting as a go-between for my GUI and my database, keeping a fair amount of the data in memory.&#160; I kept stepping through the code in debug, noticing that my Add operation did in fact add what I wanted it to, but when I got out from the object doing the adding, the TabControl didn't have a new tab item.&#160; Moreover, in debug, I could see that my main data class had not been changed, even though I thought I saw it change.&#160; Just as here, I had one copy of it changing... I just had no idea there was an auto-copy floating around, and that there is <em>no need to instantiate anything you plan on adding as a data source to your project</em>.&#160; That one took a while to sink in for me.&#160; Probably child's play for some, but it frustrated the crap out of me when I was trying to figure out why it wasn't working, and I always promise to show you the gotchas as I hit 'em.&#160; I figure either I'll help out a lot of people who are just learning this stuff too, or make myself look like an idiot.&#160; Hey, you win some, you lose some.&#160; </p>]]></content:encoded>
    </item>
    <item>
      <title>A better List for databinding</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/a-better-list-for-databinding</link>
      <pubDate>Thu, 10 Jan 2008 13:34:15 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/10/a-better-list-for-databinding/</guid>
      <description><![CDATA[Have to thank DanteG for this tip ( blogs.msdn.com/danteg ).&#160; Another quick gotcha in my quest...]]></description>
      <content:encoded><![CDATA[<p>Have to thank DanteG for this tip ( blogs.msdn.com/danteg ).&#160; Another quick gotcha in my quest for a heavily databound app.&#160; Got a code-behind data file and want to organize the data such that it binds to the contents of a listbox?&#160; Use a list -- right?&#160; Actually, that depends.&#160; </p>  <p>Will the List be changing during the use of the application?&#160; In my application, we see here a list of characters, which is databound to a List in a C# class.</p>  <p><img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_10/characterlistbox.jpg" /> </p>  <p>Clicking on the &quot;Create New&quot; button adds a new Character object to the list.&#160; To my surprise, this part of databinding didn't &quot;Just work.&quot;&#160; Just another thing that I chalk up to it all being very convenient black magic.&#160; The issue is that there was no change notification going on.&#160; I could tell through debugging that after hitting the button, there were in fact five characters in the list, but the ListBox didn't seem to care very much.... unless I did an action that caused the ListBox to be bound to a different list, and then switched back, at which point I could see my new character.</p>  <p>Well, I could go through the pain of learning how to make listener notifications, but... that's so... so... &quot;Programming HLL 1.0,&quot; as I call it.&#160; Well, maybe I should say HLL 2.0, as C is nominally a high level language, but it looks a lot like assembly to my generation of programmers (I have done both, and C isn't nearly as bad as assembly... it just felt that way after going back from a managed-memory language like Java or C#).&#160; I was &quot;born&quot; in the 2.0 langs, where you could pull this kind of thing off and do the notification, but it would be a pain.&#160; Thankfully, I now work in what I call HLL 3.0, and there is a class for just this kind of thing.&#160; </p>  <p>Realizing that, when not acting like black magic, databinding was a powerful feature of WPF, the C# gurus realized that we programmers would love to have a class that &quot;just worked&quot; with this kind of thing.&#160; </p>  <p>I introduce the ObservableCollection.&#160; It acts exactly like a List except that you can't do an AddRange operation, or anything else that manipulates a bunch of objects at once.&#160; An ObservableCollection notifies anybody databound to it of every change.&#160; In the case of my little character list, every press of the button immediately creates a new character and adds it to the ObservableCollection AND the ListBox.</p>  <p><img src="http://www.michaelmcduffee.com/picfolder/msdn/post1_10/charlistboxafteradding.jpg" /> </p>  <p>Isn't it just lovely when things work?&#160; My black magic skills have leveled up, methinks.&#160; You won't believe the dark arts needed in my next post... </p>]]></content:encoded>
    </item>
    <item>
      <title>Blend Gotcha - Databinding and IsSynchronized</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/blend-gotcha-databinding-and-issynchronized</link>
      <pubDate>Wed, 09 Jan 2008 13:42:45 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/09/blend-gotcha-databinding-and-issynchronized/</guid>
      <description><![CDATA[Working on Windows Live Writer finally (yay Live Tools), and I thought I'd share my experiences with...]]></description>
      <content:encoded><![CDATA[<p>Working on Windows Live Writer finally (yay Live Tools), and I thought I'd share my experiences with a Blend Gotcha that might take a while to figure out.&#160; I had the luxury of going and asking some of the people who wrote the features, but since many of you may not... I'll post 'em as I come across 'em.</p>  <p>This particular gotcha has to do with one of WPF and Blend's most powerful features -- Databinding.&#160; Databinding looks like the simplest thing in the world when it works, but when you're stuck on a tricky issue, it all feels like black magic.&#160; This issue isn't as tricky as it seems, but until you know the magic secret.... erg.</p>  <p>The following is a trivial application, but I came across the same issue in my super-secret project (the few details of which I mentioned earlier).&#160; In this sample app, we have two labels and two comboboxes.&#160; The labels aren't necessary to show the issue; they just help for screenshot purposes.</p>  <p><img alt="Just getting started." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/startshot.jpg" /> </p>  <p>&#160;</p>  <p>Fantastic.&#160; Now, we want the comboboxes to have a supply of possible favorite colors, and we're going to stick to a list that looks like it came out of the cheapest box of Crayolas you can find (no Burnt Umber allowed).&#160; So we go to the code-behind file and create a list.&#160; My Window1.xaml.cs now reads:</p>  <p>public partial class Window1   <br />{    <br />&#160;&#160;&#160; public Window1()    <br />&#160;&#160;&#160; {&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.InitializeComponent();    <br />&#160;&#160;&#160; } </p>  <p>} </p>  <p>public class ColorDataSource   <br />{    <br />&#160;&#160;&#160; private List&lt;string&gt; colorList; </p>  <p>&#160;&#160;&#160; public ColorDataSource()   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.colorList = new List&lt;string&gt;();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; colorList.AddRange(new string[] { &quot;Red&quot;, &quot;Orange&quot;, &quot;Yellow&quot;, &quot;Green&quot;, &quot;Blue&quot;, &quot;Purple&quot; });    <br />&#160;&#160;&#160; } </p>  <p>&#160;&#160;&#160; public List&lt;string&gt; ColorList   <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return this.colorList; }    <br />&#160;&#160;&#160; }    <br />}</p>  <p></p>  <p>Now I go back to Blend and build (building in VS won't force Blend to be using the updated files).&#160; Bobby and Timmy are picking from the same list of colors, so I want to bind the itemsource of both comboboxes to the same list.&#160; Why would I want to instantiate two lists?&#160; Such a solution would scale horribly... suppose we were using the expensive crayola box and assigning favorite colors to Bobby and Timmy's whole school.&#160; Anyhow, let's add a CLR Datasource by clicking in the data box on the + CLR Object button.</p>  <p><img height="319" alt="The Add CLR Object dialog." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/addDataSource.jpg" width="365" /> </p>  <p>We can see our new ColorDataSource object in there, so let's use that.&#160; A cool feature of Blend is that you can do databinding simply by dragging and dropping the datasource into an appropriate container!&#160; Grab the ColorList array -</p>  <p><img height="220" alt="The data window shows what you&#39;ve already added." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/expandedData.jpg" width="317" /> </p>  <p>and drop it into one of the comboboxes.&#160; Select the first option, to &quot;Bind ColorList' to 'ComboBox,'&quot; then select your field as ItemsSource (the default) as shown:</p>  <p><img height="168" alt="Pretty good default here." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/dragndrop.jpg" width="432" /> </p>  <p>and define a data template (pick what part of the ColorList you are showing in the combobox).&#160; Again, the default is what you want:</p>  <p><img height="276" alt="Fix up your data template to show what you want." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/datatemplate.jpg" width="357" /> </p>  <p>Do the same thing for the other combobox, and congrats!&#160; You've got two databound comboboxes for picking Bobby and Timmy's favorite colors.</p>  <p>&#160;<img height="116" alt="All seems to be in order." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/afterbindingboth.jpg" width="403" /> </p>  <p>So, we press F5 to test the app, and we can pick Bobby and Timmy's favorite colors.... except... that whenever I change Bobby's favorite color, Timmy's changes to match, and vice versa.&#160; That's not what I want -- is it because both boxes are bound to the same list?&#160; </p>  <p><img alt="The two kids must share one brain." src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/badTest.jpg" /> </p>  <p>Yes and no.&#160; It is <em>partially</em> caused by Bobby and Timmy's comboboxes being bound to the same list.&#160; It is also caused by a little property that I never used to pay much attention to before I came across this issue. Close the app, go back to Blend, and select one of the comboboxes.&#160; Now open up the property inspector and take a look down in the &quot;Common Properties&quot; box.&#160; The offending property is that little checkbox, &quot;IsSynchronized.&quot;</p>  <p><img alt="Sneaky, isn&#39;t it?" src="http://www.michaelmcduffee.com/picfolder/msdn/post1_19/isSynchronized.jpg" /> </p>  <p>Uncheck that box, and repeat for the other combobox.&#160; Now that neither of them is synchronized, when you test the app you can do what you wanted to do:</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/micmcd/WindowsLiveWriter/BlendGotchaDatabindingandIsSynchronized_9695/workingVersion.jpg" original-url="http://blogs.msdn.com/blogfiles/micmcd/WindowsLiveWriter/BlendGotchaDatabindingandIsSynchronized_9695/workingVersion.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="108" alt="The boys can disagree now." src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/micmcd/WindowsLiveWriter/BlendGotchaDatabindingandIsSynchronized_9695/workingVersion_thumb.jpg" original-url="http://blogs.msdn.com/blogfiles/micmcd/WindowsLiveWriter/BlendGotchaDatabindingandIsSynchronized_9695/workingVersion_thumb.jpg" width="346" border="0" /></a> </p>  <p>The world is right again... little boys can differ on their favorite colors.&#160; Even if they both picked strange colors.&#160; </p>]]></content:encoded>
    </item>
    <item>
      <title>The NiftyButton</title>
      <link>https://docs.microsoft.com/archive/blogs/micmcd/the-niftybutton</link>
      <pubDate>Wed, 02 Jan 2008 14:22:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/micmcd/2008/01/02/the-niftybutton/</guid>
      <description><![CDATA[Quick and dirty today -- a nice, easy way to create a button that looks kind of 3D, kind of nice,...]]></description>
      <content:encoded><![CDATA[<P>Quick and dirty today -- a nice, easy way to create a button that looks kind of 3D, kind of nice, and can be done very quickly, even if you throw away that ugly chrome.&nbsp; </P>
<P>The situation: you want your buttons to have a little bit of style.&nbsp; You're not an uber-artist, but you'd love the illusion of 3D, you want it to act right, and throwing away the default chrome on a button takes away the built-in nice functions.</P>
<P>Start by dropping a button on your artboard.</P>
<P><IMG title="The ordinary, ugly button." style="WIDTH: 259px; HEIGHT: 160px" height=160 alt="The ordinary, ugly button." src="http://www.michaelmcduffee.com/picfolder/msdn/lamebutton.jpg" width=259 mce_src="http://www.michaelmcduffee.com/picfolder/msdn/lamebutton.jpg"></P>
<P>You end up with this run-of-the-mill, boring gray button.&nbsp; Edit a copy of the template and delete that horrible chrome.&nbsp; Of coruse, this leaves you with an empty button, so add a grid (so you can put more than one thing in it), and inside that grid put a rectangle and a ContentPresenter.&nbsp; The rectangle is your background, and you can do with it as you wish.&nbsp; I made mine green and gave it nice, rounded edges.&nbsp; To give the illusion of 3D, give it a BitmapEffect (expand the Appearance properties area), and chose a bevel bitmap effect.&nbsp;</P>
<P mce_keep="true">&nbsp;<IMG title="Slightly more artistic now." style="WIDTH: 235px; HEIGHT: 119px" height=119 alt="Slightly more artistic now." src="http://www.michaelmcduffee.com/picfolder/msdn/greenbutton.jpg" width=235 mce_src="http://www.michaelmcduffee.com/picfolder/msdn/greenbutton.jpg"></P>
<P mce_keep="true">&nbsp;The starting position for the button should be up, so chose your EdgeProfile to be linear, and pick a small bevelwidth, like 2.&nbsp; The effect is subtle, but noticable.&nbsp; </P>
<P mce_keep="true">&nbsp;<IMG title="A raised, 3D button." style="WIDTH: 239px; HEIGHT: 123px" height=123 alt="A raised, 3D button." src="http://www.michaelmcduffee.com/picfolder/msdn/bevelbutton.jpg" width=239 mce_src="http://www.michaelmcduffee.com/picfolder/msdn/bevelbutton.jpg"></P>
<P mce_keep="true">Now select the Template, and add a property trigger.&nbsp; The "Activated when" should read target-element.IsPressed = true.&nbsp; </P>
<P>&nbsp;&nbsp;<IMG title="The property trigger info" style="WIDTH: 296px; HEIGHT: 319px" height=319 alt="The property trigger info" src="http://www.michaelmcduffee.com/picfolder/msdn/propertytrigger.jpg" width=296 mce_src="http://www.michaelmcduffee.com/picfolder/msdn/propertytrigger.jpg"></P>
<P>With the recording active (and it should be now), select your rectangle and go to the bevel property.&nbsp; Change the EdgeProfile to BulgedUp; the button now looks like something is pushing it down a bit.&nbsp; I recommend also giving a color clue.&nbsp; In my case, I made the color slightly lighter.&nbsp; </P>
<P mce_keep="true"><IMG title="The button when pressed." style="WIDTH: 224px; HEIGHT: 104px" height=104 alt="The button when pressed." src="http://www.michaelmcduffee.com/picfolder/msdn/pressedbutton.jpg" width=224 mce_src="http://www.michaelmcduffee.com/picfolder/msdn/pressedbutton.jpg"></P>
<P mce_keep="true">Now remember to shut off the property recording, go back to the main project, and build.&nbsp; The effect is nice, and was simple to create.&nbsp; You can add another property trigger for IsFocused, though I recommend a more subtle effect, like changing the stroke of the rectangle to a bright color (and thickening it a bit so you can see it), or changing the color slightly.</P>]]></content:encoded>
    </item>
  </channel>
</rss>