<?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>Code Reference &#187; Attached Properties</title>
	<atom:link href="http://sullivan.net/blog/tag/attached-properties/feed/" rel="self" type="application/rss+xml" />
	<link>http://sullivan.net/blog</link>
	<description>A collection of code for my reference (and perhaps other people too)</description>
	<lastBuildDate>Wed, 25 Jan 2012 17:53:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>UserControl as a Popup (WPF, C#) &#8211; Part 3</title>
		<link>http://sullivan.net/blog/2009/07/usercontrol-as-a-popup-wpf-c-part-3/</link>
		<comments>http://sullivan.net/blog/2009/07/usercontrol-as-a-popup-wpf-c-part-3/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 10:19:07 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Attached Properties]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=29</guid>
		<description><![CDATA[To finish off this popup, we need to be able to define it in XAML and wrap it around a block of XAML code. Why? Because defining grids and other complex items can take a LOT of coding in C#. I thougth Attached properties would be the best way to do this. Attached Properties are [...]]]></description>
			<content:encoded><![CDATA[<p>To finish off this popup, we need to be able to define it in XAML and wrap it around a block of XAML code. Why? Because defining grids and other complex items can take a LOT of coding in C#.</p>
<p>I thougth Attached properties would be the best way to do this. Attached Properties are very handy and can be used to easily add elements to existing objects.</p>
<p>Creating the PopupControl in XAML would look like this:</p>
<pre class="brush: c#">
&lt;local:PopupControl x:Name=&quot;popup2&quot; Title=&quot;Our Title&quot;&gt;
    &lt;local:PopupControl.Child&gt;
        &lt;StackPanel&gt;
            &lt;Label Content=&quot;Just a label&quot;/&gt;
        &lt;/StackPanel&gt;
    &lt;/local:PopupControl.Child&gt;
&lt;/local:PopupControl&gt;
</pre>
<p>The Attached property to enable this looks like this:</p>
<pre class="brush: c#">
public static readonly DependencyProperty ChildProperty = DependencyProperty.RegisterAttached(
    &quot;Child&quot;,
    typeof (UIElement),
    typeof (PopupControl),
    new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnChildChanged)));
public static UIElement GetChild(DependencyObject obj)
{
    return (UIElement)obj.GetValue(ChildProperty);
}
public static void SetChild(DependencyObject obj, UIElement value)
{
    obj.SetValue(ChildProperty, value);
}
private static void OnChildChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    PopupControl p = obj as PopupControl;
    if (p != null &amp;&amp; e.NewValue != null)
    {
        p.contentContainer.Children.Clear();
        p.contentContainer.Children.Add(e.NewValue as UIElement);
    }
}
</pre>
<p>This works very well.</p>
<p>However, I discovered this was not necessary. I could do the same thing with a property. Silly me.<br />
The property:</p>
<pre class="brush: c#">
public UIElement MyChild
{
    set
    {
        contentContainer.Children.Clear();
        contentContainer.Children.Add(value);
    }
}
</pre>
<p>The PopupControl in created in XMAL like this:</p>
<pre class="brush: c#">
&lt;local:PopupControl x:Name=&quot;popup3&quot;&gt;
    &lt;local:PopupControl.Title&gt;I like this title&lt;/local:PopupControl.Title&gt;
    &lt;local:PopupControl.MyChild&gt;
        &lt;StackPanel&gt;
            &lt;Label Content=&quot;Hello Mr. Bean.&quot;/&gt;
        &lt;/StackPanel&gt;
    &lt;/local:PopupControl.MyChild&gt;
&lt;/local:PopupControl&gt;
</pre>
<p>For a good article on attached properties and how they can be used, look at this article: http://en.csharp-online.net/WPF_Concepts%E2%80%94Attached_Properties</p>
<p>Now I have a popup that does everything we want it to do, but is not a popup at all. Nice!</p>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/usercontrol-as-a-popup-wpf-c-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

