<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Keith Mahoney's Silverlight Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/kmahone/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/kmahone/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:42:54 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>VisualTreeHelper</title>
      <link>https://docs.microsoft.com/archive/blogs/kmahone/visualtreehelper</link>
      <pubDate>Sun, 29 Mar 2009 22:42:00 GMT</pubDate>
      <dc:creator><![CDATA[KeithMahoney]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kmahone/2009/03/29/visualtreehelper/</guid>
      <description><![CDATA[A very useful class in Silverlight that is not as well known as it deserves to be is the...]]></description>
      <content:encoded><![CDATA[
<p>A very useful class in Silverlight that is not as well known as it deserves to be is the <a href="https://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper%28VS.95%29.aspx" mce_href="https://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper(VS.95).aspx">VisualTreeHelper</a> class. It contains four methods. One is <a href="https://msdn.microsoft.com/en-us/library/cc838402%28VS.95%29.aspx">FindElementsInHostCoordinates</a>, which is used to programatically "hit test" the visual tree and determine what elements are under a given point in the plugin. This can be very useful at times (for example in drag-and-drop type scenarios).</p>

<p>The other three methods in the class, GetParent, GetChildrenCount and GetChild (all self explanatory) are used to navigate the visual tree. Silverlight (and also WPF) has two different concepts of the tree of UI elements; the "logical" tree and the "visual" tree. Roughly, the structure of the logical tree is what you see in XAML, whereas the visual tree also includes the expanded templates of any Controls/DataTemplates, etc. So for example if you have a Button containing an Image, then the Image is the <i>logical</i> child of the Button, but the <i>visual</i> children of the Button contains all of the chrome of the Button (the ControlTemplate as defined in generic.xaml), the ContentPresenter element which displays the Image and the Image element itself.<br></p>

<p>When it comes to manipulating the visual tree, I find that this is a useful <a href="https://msdn.microsoft.com/en-us/library/bb383977.aspx">extension method</a> to have around: </p>

<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: rgb(43, 145, 175);">IEnumerable</span>&lt;<span style="color: rgb(43, 145, 175);">DependencyObject</span>&gt; GetVisuals(<span style="color: blue;">this</span> <span style="color: rgb(43, 145, 175);">DependencyObject</span> root)</p>

<p style="margin: 0px;">{</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> count = <span style="color: rgb(43, 145, 175);">VisualTreeHelper</span>.GetChildrenCount(root);</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i &lt; count; i++)</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> child = <span style="color: rgb(43, 145, 175);">VisualTreeHelper</span>.GetChild(root, i);</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">yield</span> <span style="color: blue;">return</span> child;</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> descendants <span style="color: blue;">in</span> child.GetVisuals())</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">yield</span> <span style="color: blue;">return</span> descendants;</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>

<p style="margin: 0px;">}</p>
</div>

<p>(Note the use of the <a href="https://msdn.microsoft.com/en-us/library/dscyy5s0%28VS.80%29.aspx">yield return</a> syntax; another feature that is not as widely known about as it deserves.)</p>

<p>You can use this method to search through all of the children of a given element; much easier than trying to navigate through the tree using the element's object model (e.g. examining the "Children" property of Panels, the "Content" property of ContentControls, the "Child" property of Borders, etc.). Since it deals with IEnumerables, it also plays very nicely with LINQ expressions; consider this snippet of code to disable all the controls on a page:</p>

<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> control <span style="color: blue;">in</span> LayoutRoot.GetVisuals().OfType&lt;<span style="color: rgb(43, 145, 175);">Control</span>&gt;())</p>

<p style="margin: 0px;">{</p>

<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; control.IsEnabled = <span style="color: blue;">false</span>;</p>

<p style="margin: 0px;">}</p>
</div>

<p>&nbsp;</p>

<p>VisualTreeHelper is a pretty simple class, but it can be extremely useful at times.<br></p>
]]></content:encoded>
    </item>
    <item>
      <title>Handling Mouse Input Events on Controls</title>
      <link>https://docs.microsoft.com/archive/blogs/kmahone/handling-mouse-input-events-on-controls</link>
      <pubDate>Thu, 19 Mar 2009 19:20:00 GMT</pubDate>
      <dc:creator><![CDATA[KeithMahoney]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kmahone/2009/03/19/handling-mouse-input-events-on-controls/</guid>
      <description><![CDATA[As you probably already know, Silverlight 3 Beta shipped earlier this week. There is an absolute ton...]]></description>
      <content:encoded><![CDATA[<P>As you probably already know, <A class="" title="Silverlight Beta 3" href="https://silverlight.net/getstarted/silverlight3/default.aspx" mce_href="https://silverlight.net/getstarted/silverlight3/default.aspx">Silverlight 3 Beta</A> shipped earlier this week. There is an absolute ton of new features in this release which are getting a lot of coverage; Perspective 3D, Pixel Shaders, H264 support, out of browser applications, and lots more. </P>
<P>I wanted to talk about one tiny new feature that, while it didn't recieve the&nbsp;fanfare than some of the other new features got, is still a great addition to Silverlight. What is this feature? Well, using it looks like this:</P>
<DIV style="FONT-SIZE: 8pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas">
<P style="MARGIN: 0px">element.AddHandler(<SPAN style="COLOR: #2b91af">FrameworkElement</SPAN>.MouseLeftButtonDownEvent, </P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">MouseButtonEventHandler</SPAN>(ElementMouseLeftButtonDown),</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">true</SPAN>);</P></DIV>
<P>Admittedly, it doesn't look like much; it's just an alternative syntax for registering an event hander for a mouse event (instead of the usual += syntax). But it is the last parameter, "handledEventsToo", that is interesting. This lets your event handler register to be called even for events that get marked as handled by another event handler. This allows a Silverlight application developer to get around an annoying limitation in Silverlight 2: since Controls (such as Button) mark MouseLeftButtonDown and MouseLeftButtonUp events as handled, there was no way for a Panel&nbsp;to know that one of its child Controls was clicked on. It was possible for elements such as Shapes and Images but not for Controls like Button and ListBox.</P>
<P>In WPF, this was never a problem, since it has both bubbling routed events and tunnelling routed events (aka "Preview" events); Silverlight only has bubbling events.</P>
<P>You can read more about this method in the <A class="" title="UIElement.AddHandler Method " href="https://msdn.microsoft.com/en-us/library/ms598899(VS.96).aspx" mce_href="https://msdn.microsoft.com/en-us/library/ms598899(VS.96).aspx">Silverlight 3 Beta documentation on MSDN</A></P>]]></content:encoded>
    </item>
    <item>
      <title>Introduction</title>
      <link>https://docs.microsoft.com/archive/blogs/kmahone/introduction</link>
      <pubDate>Thu, 19 Mar 2009 17:35:00 GMT</pubDate>
      <dc:creator><![CDATA[KeithMahoney]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kmahone/2009/03/19/introduction/</guid>
      <description><![CDATA[Hello,
My name is Keith Mahoney. I work on the Silverlight runtime team and I plan to use this blog...]]></description>
      <content:encoded><![CDATA[<P>Hello,</P>
<P>My name is Keith Mahoney. I work on the Silverlight runtime team and I plan to use this blog to post bits of information related to Silverlight. No specific theme; just things that I think others would find interesting or useful. </P>
<P>And that's it for my introductory post! </P>
<P>Keith.</P>]]></content:encoded>
    </item>
  </channel>
</rss>