<?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>Veracity Solutions Blog &#187; Slider</title>
	<atom:link href="http://blogs.veracitysolutions.com/tag/slider/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.veracitysolutions.com</link>
	<description>Software Consulting That Rocks</description>
	<lastBuildDate>Thu, 22 Apr 2010 17:21:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create A Snapping Slider In Blend Using Behaviors (Silverlight 3 or WPF)</title>
		<link>http://blogs.veracitysolutions.com/create-a-snapping-slider-in-blend-using-behaviors-silverlight-3-or-wpf/</link>
		<comments>http://blogs.veracitysolutions.com/create-a-snapping-slider-in-blend-using-behaviors-silverlight-3-or-wpf/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 02:59:08 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Behaviors]]></category>
		<category><![CDATA[Rounding Slider]]></category>
		<category><![CDATA[Silverlight 3]]></category>
		<category><![CDATA[Slider]]></category>
		<category><![CDATA[Snapping Slider]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=650</guid>
		<description><![CDATA[UPDATE: It turns out that in WPF, there is an easier way to snap the slider. For integers, simply check the &#8220;IsSnapToTickEnabled&#8221; and set your TickFrequency accordingly. Which means that this behavior is really only useful for Silverlight, which doesn&#8217;t have those properties.
Behaviors are easily the coolest thing that has ever happened to anything. 
Perhaps [...]]]></description>
			<content:encoded><![CDATA[<p>UPDATE: It turns out that in WPF, there is an easier way to snap the slider. For integers, simply check the &#8220;IsSnapToTickEnabled&#8221; and set your TickFrequency accordingly. Which means that this behavior is really only useful for Silverlight, which doesn&#8217;t have those properties.</p>
<p>Behaviors are easily the coolest thing that has ever happened to anything. </p>
<p>Perhaps I overstate my case somewhat. But they’re still pretty cool.</p>
<p>To me, a wonderful example is the ability to let designers/developers get some really cool functionality out of the Silverlight or WPF slider.</p>
<p>Now, one might normally think that there is a simple way to make your slider values to snap to an integer or to some incremental number (perhaps base 2 or base 10). Alas, one would be wrong. There have been a couple solutions to this problem (see <a href="http://blog.webjak.net/2009/05/10/silverlight-slider-that-snaps-to-rounded-value-when-dragged/">here</a> and <a href="http://blogs.msdn.com/devdave/archive/2008/06/12/discreteslider-a-simple-control-subclass.aspx">here</a>), but they usually involve creating a new slider subclass with the additional functionality if you really want the interaction to be reusable.</p>
<p>However, behaviors makes all this unnecessary. What is even better is that there is no code difference between WPF and Silverlight on this one. That’s right… this tutorial and the code that goes with it works exactly the same in Silverlight as it does in WPF. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/09/SnappingSlider.cs">SnappingSlider Behavior (SnappingSlider.cs)</a> – Works for Silverlight and WPF</p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/09/SnappingSliderWPF.zip'>WPF SnappingSlider Behavior (All Project Files)</a></p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/09/SnappingSliderSilverlight.zip'>Silverlight SnappingSlider Behavior (All Project Files)</a></p>
<p>You can also <a href="http://gallery.expression.microsoft.com/en-us/SnappingSlider">download the behavior here (just the cs file)</a> which would make me happy because it ups my street cred among the Silverlight homies (says the whitest man on earth).</p>
<p>If you want to learn how to create it yourself, let’s get started.</p>
<p>In your project in Blend, right-click on the project and go to “Add New Item…” </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/09/clip_image0014.png"><img style="border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px" border="0" alt="clip_image001[4]" src="http://www.designersilverlight.com/wp-content/uploads/2009/09/clip_image0014_thumb.png" width="279" height="81" /></a></p>
<p>Choose “Behavior” from the list and type in the name of the behavior you want to create</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/09/clip_image0016.png"><img style="border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px" border="0" alt="clip_image001[6]" src="http://www.designersilverlight.com/wp-content/uploads/2009/09/clip_image0016_thumb.png" width="381" height="176" /></a></p>
<p>This is where Blend is way cooler than Visual Studio… it gives you pretty much all the code and imports all the appropriate references for creating a behavior. So, first change the behavior associated object type to a Slider:</p>
<p><span style="color: blue">public class </span><span style="color: black">SnappingSlider : Behavior&lt;Slider&gt;</span></p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p> And go into the inside the OnAttached method, enter the following code:</p>
<p><span style="color: black">AssociatedObject.ValueChanged +=</span><span style="color: blue">new </span><span style="color: black">System.Windows.RoutedPropertyChangedEventHandler&lt;</span><span style="color: blue">double</span><span style="color: black">&gt;(AssociatedObject_ValueChanged);</span></p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p> Hint to Code Newbies: If you get to the “+=” part and hit the Tab key twice it will add the necessary code.</p>
<p>Inside the AssociatedObject_ValueChanged method, I added the following code: </p>
<p><span style="color: blue">private void</span><span style="color: black">AssociatedObject_ValueChanged(</span><span style="color: blue">object</span><span style="color: black">sender, System.Windows.RoutedPropertyChangedEventArgs&lt;</span><span style="color: blue">double</span><span style="color: black">&gt; e)     <br />{      <br />&#160;&#160; </span><span style="color: green">//Let the slider be equal to the Maximum and Minimum values     <br />&#160;&#160; </span><span style="color: blue">if</span><span style="color: black">((e.NewValue != AssociatedObject.Maximum) &amp;&amp; (e.NewValue != AssociatedObject.Minimum))     <br />&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: green">//Using the Minimum value as a starting point, only allow values that are      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //&#160;&#160; a multiple of the SmallChange value. If you want to simply round to       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //&#160;&#160; integers, set it so that:       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //&#160;&#160; SmallChange = 1      <br />&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">double</span><span style="color: black">calcValue = Math.Floor((e.NewValue &#8211; AssociatedObject.Minimum) / AssociatedObject.SmallChange);     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; calcValue = (calcValue * AssociatedObject.SmallChange) + AssociatedObject.Minimum;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; AssociatedObject.Value = Math.Round(calcValue);      <br />&#160;&#160;&#160; }      <br />&#160;&#160;&#160; </span><span style="color: blue">else     <br />&#160;&#160;&#160; </span><span style="color: black">{     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: green">//Sometimes it hiccups on me, so I used this to catch those     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: black">AssociatedObject.Value = Math.Round(e.NewValue);     <br />&#160;&#160;&#160; }      <br />}</span></p>
<p> <a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a>
<p>Once you have that code in place, build the project (menu option Project –&gt; Build Project).</p>
<p>Now you should have the behavior available in the “Assets” tab under “Behaviors” (at the bottom of the picture below).</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/09/clip_image0018.png"><img style="border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px" border="0" alt="clip_image001[8]" src="http://www.designersilverlight.com/wp-content/uploads/2009/09/clip_image0018_thumb.png" width="349" height="242" /></a></p>
<p>&#160;</p>
<p>Just drag it onto your Slider and set the Minimum, Maximum and SmallChange properties appropriately. Remember that SmallChange is the value is that is acting as your increment controller. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/create-a-snapping-slider-in-blend-using-behaviors-silverlight-3-or-wpf/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
