<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Attached Properties</title>
	<atom:link href="http://joelbennett.net/tag/attached-properties/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerBoots and Attached Properties</title>
		<link>http://joelbennett.net/powerboots-and-attached-properties/</link>
		<comments>http://joelbennett.net/powerboots-and-attached-properties/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 05:53:49 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Attached Properties]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1055</guid>
		<description><![CDATA[WPF uses a concept called &#8220;Attached Properties&#8221; to handle certain things, like when you put controls into a DockPanel. Basically, anything you put inside a DockPanel has a property &#8220;Dock&#8221; which you can set &#8230; but because the property is actually defined by the DockPanel, it doesn&#8217;t show up in PowerBoots, so you can&#8217;t just [...]]]></description>
			<content:encoded><![CDATA[	<p><span class="caps">WPF</span> uses a concept called &#8220;Attached Properties&#8221; to handle certain things, like when you put controls into a DockPanel. Basically, anything you put inside a DockPanel has a property &#8220;Dock&#8221; which you can set &#8230; but because the property is actually defined by the DockPanel, it doesn&#8217;t show up in PowerBoots, so you can&#8217;t just say <code>-Dock &#34;Bottom&#34;</code> when you create the child items &#8230; at least, not yet.</p>

	<p>I&#8217;m still thinking about the best way for me to expose this in PowerBoots, because the problem is that attached properties can get attached several ways, and not just on direct children of an element. So I wanted to show you a way that you can do it <em>for now</em> so you can at least use the DockPanel effectively:</p>

	<div class="posh code posh" style="font-family:monospace;">Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;2,0,2,0&quot;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Dock</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Right</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-HorizontalAlignment</span> Stretch<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Dock</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Bottom</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-AcceptsReturn</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #000066;">-minWidth</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-minHeight</span> <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h2>A better way &#8230;</h2>

	<p>Here&#8217;s what I&#8217;m thinking about for the next release of PowerBoots (you can take this and use it now, if you like it, but I&#8217;m really looking for, uhm &#8230; better ideas).  Basically, I have written a function: <code>Set-AttachedProperty</code>, which takes an attached property and a value, and passes through the element on the pipeline. You may want to use this in conjunction with the module I published awhile back for creating type accelerators, because it lets you run a line like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Accelerator</span></span> DockPanel System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span></div>

	<p>Which will let you substitute <code>[DockPanel]</code> for <code>[System.Windows.Controls.DockPanel]</code> &#8230; and of course, you can use it for all the types you want to use attached properties from, and it&#8217;s a real lifesaver if you need to use a bunch of them.  Of course, in this particular example, we really only need to use a single attached property, so it&#8217;s enough to define that property ahead of time:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$DockProperty</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span></div>

	<p>Then you can use the <code>Set-AttachedProperty</code> function through an alias <code>sap</code>, and rewrite that huge block above like this:</p>

	<div class="posh code posh" style="font-family:monospace;">Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;2,0,2,0&quot;</span> <span style="color: #66cc66;">|</span> sap <span style="color: #660033; font-weight: bold;">$DockProperty</span> Right<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-HorizontalAlignment</span> Stretch<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> sap <span style="color: #660033; font-weight: bold;">$DockProperty</span> Bottom<br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-AcceptsReturn</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #000066;">-minWidth</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-minHeight</span> <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Pretty slick, right? And it will even print out the list of values in the error message if you invoke it with an invalid value against an enum property like dock:  <code>sap $DockProperty &#34;&#34;</code> &#8230; The problem I have with it is that you have to predefine your <code>$DockProperty</code> variable, and you can&#8217;t just define it against the root class.  So I&#8217;m trying to find a way to tweak the dynamic property generation to make it so that the pipe into <code>| sap $DockProperty Bottom</code>  can just be a parameter to the original element: <code>-Dock Bottom</code> &#8230; if I can&#8217;t find that, in the worst case scenario, I&#8217;ll just add an -AttachedProperties parameter with a hashtable of $DockProperty,&#8220;Bottom&#8221; or something &#8230; <em>what do you think?</em>  </p>

	<p>In any case, here&#8217;s the sap function, for now, and remember to use it as part of the pipeline:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AttachedProperty</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">DependencyProperty</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Property</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">DYNAMICPARAM</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$paramDictionary</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">RuntimeDefinedParameterDictionary</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">RuntimeDefinedParameter</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Name</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Value&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># $Param1.Attributes.Add( (New-ParameterAttribute -Position 1) )</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Attributes</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ParameterAttribute</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> Position <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">ParameterType</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Property</span>.<span style="color: #003366;">PropertyType</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$paramDictionary</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Value&quot;</span>, <span style="color: #660033; font-weight: bold;">$Param1</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$paramDictionary</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Property</span>, <span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Value</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Alias</span></span> sap <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AttachedProperty</span></span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-and-attached-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

