<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Lightweight Drawing with DrawingVisuals: Part 2 of 2</title>
	<atom:link href="http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/</link>
	<description>Tips, tricks, and howto&#039;s for those &#34;WTF?&#34; moments in WPF</description>
	<lastBuildDate>Thu, 09 Jul 2009 11:10:16 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: jimagination</title>
		<link>http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-19</link>
		<dc:creator>jimagination</dc:creator>
		<pubDate>Wed, 24 Sep 2008 07:14:48 +0000</pubDate>
		<guid isPermaLink="false">http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-19</guid>
		<description>sorry for the last post i guess i just cannot add xaml code directly anyhow what i have is this:

    Grid DataContext=&quot;TestData&quot;
        Testpanel Loaded=&quot;Testpanel_Loaded&quot;
            Testpanel.Content
                Button Content=&quot;{Binding}&quot;</description>
		<content:encoded><![CDATA[<p>sorry for the last post i guess i just cannot add xaml code directly anyhow what i have is this:</p>
<p>    Grid DataContext=&#8221;TestData&#8221;<br />
        Testpanel Loaded=&#8221;Testpanel_Loaded&#8221;<br />
            Testpanel.Content<br />
                Button Content=&#8221;{Binding}&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jimagination</title>
		<link>http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-17</link>
		<dc:creator>jimagination</dc:creator>
		<pubDate>Wed, 24 Sep 2008 06:28:49 +0000</pubDate>
		<guid isPermaLink="false">http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-17</guid>
		<description>Hi Jerry,
I ran into some problems after implementing the code above and i&#039;ll be glad if you could give me a hand. My problem is not directly connected to your implementation of the squarevisual class. 
See i need a lightweight class to pass that draws one visual and passes datacontext to it. I&#039;ve implemented a pseudo contentcontrol following your directions above and inheriting directly from frameworkelement, because i strictly need my control not to to have a template. Here is my implementation:
class Testpanel : FrameworkElement
	{

		public Visual Content
		{
			get { return (Visual) GetValue(ContentProperty); }
			set { SetValue(ContentProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ContentProperty =
			DependencyProperty.Register(&quot;Content&quot;, typeof(Visual), typeof(Testpanel), new FrameworkPropertyMetadata(null, ContentChanged));

		private static void ContentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
			(sender as Testpanel).HandleContentChanged(e.NewValue, e.OldValue);
		}

		protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e);
		}

		private void HandleContentChanged(object p, object p_2)
		{
			this.AddLogicalChild(p);
			this.AddVisualChild(p as Visual);
		}

		protected override Visual GetVisualChild(int index)
		{
			return this.Content != null ? this.Content : null;
		}

		protected override int VisualChildrenCount
		{
			get
			{
				return this.Content != null ? 1 : 0;
			}
		}

		protected override Size ArrangeOverride(Size finalSize)
		{
			(this.Content as FrameworkElement).Arrange(new Rect(new Point(0, 0), finalSize));
			return finalSize;
		}

		protected override Size MeasureOverride(Size availableSize)
		{
			(this.Content as FrameworkElement).Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
			return availableSize;
		}

	}

So far so good! i call it from xaml as so:

 
        
            
                
            
              
    

Now without the Loaded event handler it works as expected the button&#039;s content is &quot;TestData&quot;. now in the Testpanel_Loaded event handler i try and change the testpanel&#039;s DataContext  and nothing changes as it would if Testpanel inherited from ContentControl or even Content for that matter. I&#039;ve added a DataContext propertycallback in i custom button that i&#039;ve added as content and what i discovered was that the DataContext changes but somehow the button&#039;s binding does not happen. Do you have any ideas why this is so or what is it that the Control adds up to the FrameworkElement that it makes the binding magic happen?
thanx in advance
jim</description>
		<content:encoded><![CDATA[<p>Hi Jerry,<br />
I ran into some problems after implementing the code above and i&#8217;ll be glad if you could give me a hand. My problem is not directly connected to your implementation of the squarevisual class.<br />
See i need a lightweight class to pass that draws one visual and passes datacontext to it. I&#8217;ve implemented a pseudo contentcontrol following your directions above and inheriting directly from frameworkelement, because i strictly need my control not to to have a template. Here is my implementation:<br />
class Testpanel : FrameworkElement<br />
	{</p>
<p>		public Visual Content<br />
		{<br />
			get { return (Visual) GetValue(ContentProperty); }<br />
			set { SetValue(ContentProperty, value); }<br />
		}</p>
<p>		// Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc&#8230;<br />
		public static readonly DependencyProperty ContentProperty =<br />
			DependencyProperty.Register(&#8220;Content&#8221;, typeof(Visual), typeof(Testpanel), new FrameworkPropertyMetadata(null, ContentChanged));</p>
<p>		private static void ContentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)<br />
		{<br />
			(sender as Testpanel).HandleContentChanged(e.NewValue, e.OldValue);<br />
		}</p>
<p>		protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)<br />
		{<br />
			base.OnPropertyChanged(e);<br />
		}</p>
<p>		private void HandleContentChanged(object p, object p_2)<br />
		{<br />
			this.AddLogicalChild(p);<br />
			this.AddVisualChild(p as Visual);<br />
		}</p>
<p>		protected override Visual GetVisualChild(int index)<br />
		{<br />
			return this.Content != null ? this.Content : null;<br />
		}</p>
<p>		protected override int VisualChildrenCount<br />
		{<br />
			get<br />
			{<br />
				return this.Content != null ? 1 : 0;<br />
			}<br />
		}</p>
<p>		protected override Size ArrangeOverride(Size finalSize)<br />
		{<br />
			(this.Content as FrameworkElement).Arrange(new Rect(new Point(0, 0), finalSize));<br />
			return finalSize;<br />
		}</p>
<p>		protected override Size MeasureOverride(Size availableSize)<br />
		{<br />
			(this.Content as FrameworkElement).Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));<br />
			return availableSize;<br />
		}</p>
<p>	}</p>
<p>So far so good! i call it from xaml as so:</p>
<p>Now without the Loaded event handler it works as expected the button&#8217;s content is &#8220;TestData&#8221;. now in the Testpanel_Loaded event handler i try and change the testpanel&#8217;s DataContext  and nothing changes as it would if Testpanel inherited from ContentControl or even Content for that matter. I&#8217;ve added a DataContext propertycallback in i custom button that i&#8217;ve added as content and what i discovered was that the DataContext changes but somehow the button&#8217;s binding does not happen. Do you have any ideas why this is so or what is it that the Control adds up to the FrameworkElement that it makes the binding magic happen?<br />
thanx in advance<br />
jim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jerry Lin</title>
		<link>http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-9</link>
		<dc:creator>Jerry Lin</dc:creator>
		<pubDate>Mon, 11 Feb 2008 13:53:28 +0000</pubDate>
		<guid isPermaLink="false">http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-9</guid>
		<description>Hi Rob,

Yes, it is possible to animate the square. Remember that DrawingVisuals are lightweight, so they won&#039;t necessarily have the properties that you want to animate. In this case, the square doesn&#039;t have Top and Left properties to move the square.

One workaround is to calculate where you want the square to move wrt to its host, and move it incrementally every time CompositionTarget.Rendering is called. I would avoid this though because it is called for every single frame that is about to be rendered and can be CPU intensive.

Another way off the top of my head would be to host the square inside the Canvas panel. This way you can take advantage of the Canvas.Left and Canvas.Top attached properties and animate them for the square. 

Of if you like to reinvent the wheel, you can declare similar attached properties inside the FrameworkElement that is hosting your visuals. But be sure to declare the properties with the AffectsParentArrange and AffectsRender metadata flags. Then implement ArrangeOverride so that the square&#039;s top-left is placed wrt to the values of those properties. Now you can animate the square&#039;s position inside XAML using Storyboards. This is similar to how the Sun launcher is animated in Jing when the cursor is on top of it. (but the balls are restyled buttons instead of being DrawingVisuals).

Sorry for the lengthy response. This should be another post that I explore in the future.

Jerry</description>
		<content:encoded><![CDATA[<p>Hi Rob,</p>
<p>Yes, it is possible to animate the square. Remember that DrawingVisuals are lightweight, so they won&#8217;t necessarily have the properties that you want to animate. In this case, the square doesn&#8217;t have Top and Left properties to move the square.</p>
<p>One workaround is to calculate where you want the square to move wrt to its host, and move it incrementally every time CompositionTarget.Rendering is called. I would avoid this though because it is called for every single frame that is about to be rendered and can be CPU intensive.</p>
<p>Another way off the top of my head would be to host the square inside the Canvas panel. This way you can take advantage of the Canvas.Left and Canvas.Top attached properties and animate them for the square. </p>
<p>Of if you like to reinvent the wheel, you can declare similar attached properties inside the FrameworkElement that is hosting your visuals. But be sure to declare the properties with the AffectsParentArrange and AffectsRender metadata flags. Then implement ArrangeOverride so that the square&#8217;s top-left is placed wrt to the values of those properties. Now you can animate the square&#8217;s position inside XAML using Storyboards. This is similar to how the Sun launcher is animated in Jing when the cursor is on top of it. (but the balls are restyled buttons instead of being DrawingVisuals).</p>
<p>Sorry for the lengthy response. This should be another post that I explore in the future.</p>
<p>Jerry</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robfiregarden</title>
		<link>http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-8</link>
		<dc:creator>robfiregarden</dc:creator>
		<pubDate>Sun, 10 Feb 2008 03:08:44 +0000</pubDate>
		<guid isPermaLink="false">http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/#comment-8</guid>
		<description>Is it possible to animate properties of this square? Such that we can make it move around the screen?

Rob

http://www.robsword.net</description>
		<content:encoded><![CDATA[<p>Is it possible to animate properties of this square? Such that we can make it move around the screen?</p>
<p>Rob</p>
<p><a href="http://www.robsword.net" rel="nofollow">http://www.robsword.net</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
