SilverlightShow: Silverlight Community http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples en-us SilverlightShow.net estoychev@completit.com (Emil Stoychev) Argotic Syndication Framework, http://www.codeplex.com/Argotic http://www.rssboard.org/rss-specification Tip: Using Blur and DropShadow effects in Silverlight 3 http://www.silverlightshow.net/tips/Using-Blur-and-DropShadow-effects-in-Silverlight-3.aspx <em>Silverlight 3</em> comes with two built in <em>Pixel Shaders</em>:<br /> <br /> We have the following image:<br /> <br /> <img alt="" src="http://www.silverlightshow.net/Storage/Users/nikolayraychev/pixel_shaders_1.jpg" /><br /> <br /> We want to blur it:<br /> <br /> <div style="line-height: 100% !important; background-color: white; font-family: courier new; font-size: 11px; overflow: auto;border: rgb(127,157,185) 1px solid;"> <table style="border-bottom: rgb(238,238,238) 0px solid; border-right-width: 0px; background-color: rgb(255,255,255); margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: rgb(247,247,247) 1px solid; padding-left: 10px; font-family: courier new; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px;"></span><span style="color: #0000ff;"><</span><span style="font-size: 11px;">Image </span><span style="color: #ff0000;">Width</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"300"</span><span style="font-size: 11px;">   </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);">    <span style="color: #ff0000;">Source</span><span style="font-size: 11px;">="http://terraristic.net/photos/  </span></td> </tr> <tr> <td>    Brachypelma_albiceps/Brachypelma_albiceps_1.jpg"<span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">    <span style="color: #0000ff;"><</span><span style="font-size: 11px;">Image.Effect</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">        <span style="color: #0000ff;"><</span><span style="font-size: 11px;">BlurEffect </span><span style="color: #ff0000;">Radius</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"8"</span><span style="font-size: 11px;">></span><span style="color: #0000ff;"></</span><span style="font-size: 11px;">BlurEffect</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">    <span style="color: #0000ff;"></</span><span style="font-size: 11px;">Image.Effect</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td><span style="color: #0000ff;"></</span><span style="font-size: 11px;">Image</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> </tbody> </table> </div> <br /> We have the following result:<br /> <br /> <img alt="" src="http://www.silverlightshow.net/Storage/Users/nikolayraychev/pixel_shaders_2.jpg" /><br /> <br /> Note the <em>Radius</em> parameter. The bigger the radius is, the more blurred the picture is.<br /> <br /> Now let's make a shadowed picture:<br /> <br /> <div style="line-height: 100% !important; background-color: white; font-family: courier new; font-size: 11px; overflow: auto;border: rgb(127,157,185) 1px solid;"> <table style="border-bottom: rgb(238,238,238) 0px solid; border-right-width: 0px; background-color: rgb(255,255,255); margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: rgb(247,247,247) 1px solid; padding-left: 10px; font-family: courier new; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px;"></span><span style="color: #0000ff;"><</span><span style="font-size: 11px;">Image </span><span style="color: #ff0000;">Width</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"300"</span><span style="font-size: 11px;">   </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);">    <span style="color: #ff0000;">Source</span><span style="font-size: 11px;">="http://terraristic.net/photos/  </span></td> </tr> <tr> <td>    Brachypelma_albiceps/Brachypelma_albiceps_1.jpg"<span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">    <span style="color: #0000ff;"><</span><span style="font-size: 11px;">Image.Effect</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">        <span style="color: #0000ff;"><</span><span style="font-size: 11px;">DropShadowEffect </span><span style="color: #ff0000;">BlurRadius</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"30"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Gray"</span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">            <span style="color: #ff0000;">Direction</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"-45"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Opacity</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0.5"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">ShadowDepth</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"20"</span><span style="font-size: 11px;">></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">        <span style="color: #0000ff;"></</span><span style="font-size: 11px;">DropShadowEffect</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">    <span style="color: #0000ff;"></</span><span style="font-size: 11px;">Image.Effect</span><span style="color: #0000ff;">></span></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td><span style="color: #0000ff;"></</span><span style="font-size: 11px;">Image</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> </tbody> </table> </div> <br /> The result:<br /> <br /> <img alt="" src="http://www.silverlightshow.net/Storage/Users/nikolayraychev/pixel_shaders_3.jpg" /> <br /> <br /> The <em>DropShadow</em> effect has several parameters:<br /> <ul> <li><em>BlurRadius</em> - the bigger the radius is, the more blurred the shadow is. </li> <li><em>Color</em> - the shadow color. </li> <li><em>Direction</em> - an angle specifying the direction of the shadow. If you set it to zero the shadow will fall on the right side. </li> <li><em>Opacity</em> - the shadow opacity. </li> <li><em>ShadowDepth</em> - it specifies how far (deep) from the picture the shadow will appear. </li> </ul> <p>You can define your own <em>Pixel Shaders</em> using a special language called <em>HLSL</em>. But this is beyond the scope of this tip.</p> <p>That's it!</p> editorial@silverlightshow.net ( Nikolay Raychev) http://www.silverlightshow.net/tips/Using-Blur-and-DropShadow-effects-in-Silverlight-3.aspx#comments http://www.silverlightshow.net/tips/Using-Blur-and-DropShadow-effects-in-Silverlight-3.aspx Thu, 02 Apr 2009 07:35:00 +0200 Tip: Enable multiple selection in Silverlight 3 ListBox http://www.silverlightshow.net/tips/Enable-multiple-selection-in-Silverlight-ListBox.aspx In <em>Silverlight 3</em> you can make multiple selections in a <em>ListBox</em>. You just need to set the <em>SelectionMode</em> parameter:<br /> <br /> <div style="line-height: 100% !important; background-color: white; font-family: courier new; font-size: 11px; overflow: auto;border: rgb(127,157,185) 1px solid;"> <table style="border-bottom: rgb(238,238,238) 0px solid; border-right-width: 0px; background-color: rgb(255,255,255); margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px; undefined: ;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: rgb(247,247,247) 1px solid; padding-left: 10px; font-family: courier new; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody class="RadEDomMouseOver"> <tr> <td><span style="font-size: 11px;"></span><span style="color: #0000ff;"><</span><span style="font-size: 11px;">ListBox </span><span style="color: #ff0000;">Margin</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"5"</span><span style="font-size: 11px;"> x:</span><span style="color: #ff0000;">Name</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"lbTasks"</span><span style="font-size: 11px;">   </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247); undefined: ;">    <span style="color: #ff0000;">ItemsSource</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"{Binding Tasks, ElementName=MainPageView}"</span><span style="font-size: 11px;">   </span></td> </tr> <tr> <td>    <span style="background-color: rgb(255,255,153);"><span style="color: #ff0000;">SelectionMode</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Multiple"</span></span><span style="font-size: 11px;">>  </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247); undefined: ;">    <span style="color: #0000ff;"><</span><span style="font-size: 11px;">ListBox.ItemTemplate</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td>        <span style="color: #0000ff;"><</span><span style="font-size: 11px;">DataTemplate</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247); undefined: ;">            <span style="color: #0000ff;"><</span><span style="font-size: 11px;">StackPanel </span><span style="color: #ff0000;">Orientation</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Horizontal"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Margin</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"2"</span><span style="font-size: 11px;">>  </span></td> </tr> <tr> <td>                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">TextBlock </span><span style="color: #ff0000;">FontWeight</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Bold"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">FontSize</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"13"</span><span style="font-size: 11px;">   </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247); undefined: ;">                    <span style="color: #ff0000;">Foreground</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"#ff006882"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Text</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"{Binding Text}"</span><span style="font-size: 11px;">>  </span></td> </tr> <tr> <td>                <span style="color: #0000ff;"></</span><span style="font-size: 11px;">TextBlock</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247); undefined: ;">            <span style="color: #0000ff;"></</span><span style="font-size: 11px;">StackPanel</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td>        <span style="color: #0000ff;"></</span><span style="font-size: 11px;">DataTemplate</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247); undefined: ;">    <span style="color: #0000ff;"></</span><span style="font-size: 11px;">ListBox.ItemTemplate</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td><span style="color: #0000ff;"></</span><span style="font-size: 11px;">ListBox</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> </tbody> </table> </div> <br /> You have 3 options for the SelectionMode:<br /> <ul> <li><em>Single</em> - you can select only one item. </li> <li><em>Multiple</em> - you can select multiple items by selecting one item, holding <em>Ctrl</em> or <em>Shift</em> key and pressing another item. </li> <li><em>Extended</em> - you can again select multiple items but the <em>Shift</em> key acts differently. With the help of the Shift key you can select items range by just pressing one item, holding <em>Shift</em> and pressing another one. </li> </ul> <p>That's it!</p> editorial@silverlightshow.net ( Nikolay Raychev) http://www.silverlightshow.net/tips/Enable-multiple-selection-in-Silverlight-ListBox.aspx#comments http://www.silverlightshow.net/tips/Enable-multiple-selection-in-Silverlight-ListBox.aspx Thu, 02 Apr 2009 07:32:00 +0200 Tip: How to style the caret in Silverlight 3 http://www.silverlightshow.net/tips/How-to-style-the-caret-in-Silverlight-3.aspx It's very easy to style the caret in <em>Silverlight 3</em>. Look at that huge <em>TextBox</em> with a caret in the rainbow colors:<br /> <br /> <img alt="" src="http://www.silverlightshow.net/Storage/Users/nikolayraychev/caret_brush.gif" /><br /> <br /> You just need to set the <em>CaretBrush</em> property:<br /> <br /> <div style="line-height: 100% !important; overflow-x: auto; overflow-y: auto; background-color: white; font-family: 'courier new'; font-size: 11px;border: rgb(127,157,185) 1px solid;"> <table style="border-bottom: rgb(238,238,238) 0px solid; border-right-width: 0px; background-color: rgb(255,255,255); margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: rgb(247,247,247) 1px solid; padding-left: 10px; font-family: 'courier new'; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px;"></span><span style="color: #0000ff;"><</span><span style="font-size: 11px;">TextBox </span><span style="color: #ff0000;">FontSize</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"50"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Width</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"100"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Height</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"80"</span><span style="font-size: 11px;">>  </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);">    <span style="color: #0000ff;"><</span><span style="font-size: 11px;">TextBox.RenderTransform</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td>        <span style="color: #0000ff;"><</span><span style="font-size: 11px;">ScaleTransform </span><span style="color: #ff0000;">ScaleX</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"6"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">ScaleY</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"1"</span><span style="font-size: 11px;">/>  </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);">    <span style="color: #0000ff;"></</span><span style="font-size: 11px;">TextBox.RenderTransform</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">    <span style="color: #0000ff;"><</span><span style="font-size: 11px;">TextBox.CaretBrush</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">        <span style="color: #0000ff;"><</span><span style="font-size: 11px;">LinearGradientBrush x:</span><span style="color: #ff0000;">Name</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"backgroundLinearGradientBrush"</span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">            <span style="color: #ff0000;">MappingMode</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"RelativeToBoundingBox"</span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">                <span style="color: #ff0000;">StartPoint</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0,0"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">EndPoint</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0,1"</span><span style="font-size: 11px;">></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">            <span style="color: #0000ff;"><</span><span style="font-size: 11px;">LinearGradientBrush.GradientStops</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Red"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0"</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">/></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Orange"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0.167"</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">/></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Yellow"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0.333"</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">/></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Green"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0.5"</span><span style="font-size: 11px;">/> </span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Blue"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0.667"</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">/></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Indigo"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"0.833"</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">/></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">                <span style="color: #0000ff;"><</span><span style="font-size: 11px;">GradientStop </span><span style="color: #ff0000;">Color</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"Violet"</span><span style="font-size: 11px;"> </span><span style="color: #ff0000;">Offset</span><span style="font-size: 11px;">=</span><span style="color: #0000ff;">"1"</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">/></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">            <span style="color: #0000ff;"></</span><span style="font-size: 11px;">LinearGradientBrush.GradientStops</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="background-color: rgb(255,255,153);">        <span style="color: #0000ff;"></</span><span style="font-size: 11px;">LinearGradientBrush</span><span style="color: #0000ff;">></span></span></td> </tr> <tr> <td><span style="background-color: rgb(255,255,153);">    <span style="color: #0000ff;"></</span><span style="font-size: 11px;">TextBox.CaretBrush</span><span style="color: #0000ff;">></span></span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: rgb(247,247,247);"><span style="color: #0000ff;"></</span><span style="font-size: 11px;">TextBox</span><span style="color: #0000ff;">></span><span style="font-size: 11px;"> </span></td> </tr> </tbody> </table> </div> <br /> You can use any <em>Silverlight</em> supported <em>Brush</em>.<br /> <br /> That's it! editorial@silverlightshow.net ( Nikolay Raychev) http://www.silverlightshow.net/tips/How-to-style-the-caret-in-Silverlight-3.aspx#comments http://www.silverlightshow.net/tips/How-to-style-the-caret-in-Silverlight-3.aspx Wed, 25 Mar 2009 10:46:00 +0200 Tip: XAML Element Binding http://www.silverlightshow.net/tips/XAML-Element-Binding.aspx <p>Silverlight 3 enables property binding to CLR objects and other UI components via XAML – UI to UI binding. It is useful in a lot of scenarios and saves time for both developers and designers.</p> <h3>Example</h3> <object width="410" height="330" data="data:application/x-silverlight-2," type="application/x-silverlight-2"> <param name="source" value="http://www.silverlightshow.net/storage/XAMLElementBinding.xap"/> <param name="onerror" value="onSilverlightError"/> <param name="background" value="white"/> <param name="minRuntimeVersion" value="3.0.40624.0"/> <param name="autoUpgrade" value="true"/> <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none;"> </a></object> <p><a href="http://www.silverlightshow.net/showcase/XAMLElementBinding.zip" shape="rect">Download source code</a></p> <h3>Syntax</h3> <div> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{Binding Value, ElementName=MySlider}</pre> </div> <p>where <em>Value</em> is property of a CLR object and <em>MySlider</em> is the name of this object</p> <p>Hope that helps!</p> <p>That's it!</p> editorial@silverlightshow.net ( Emil Stoychev) http://www.silverlightshow.net/tips/XAML-Element-Binding.aspx#comments http://www.silverlightshow.net/tips/XAML-Element-Binding.aspx Tue, 24 Mar 2009 00:06:00 +0200 Tip: Cascading Styles in Silverlight 3 http://www.silverlightshow.net/tips/Tip-Cascading-Styles-in-Silverlight-3.aspx <p class="MsoNormal"> </p> <p class="MsoNormal"><span style="font-family: 'courier new'; color: #0000ff; font-size: 10pt;"></span></p> Like in CSS you can now cascade styles in Silverlight 3 too. You can append and/or override values by basing one style on another. In this way you can built a powerful and reusable set of styles to apply in your Silverlight applications. <p> </p> <h3>Syntax</h3> <p>Style cascading is accomplished by using the new BasedOn attribute of the Style class.</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 1:</span> <Style x:Name=<span style="color: #006080;">"Headline"</span> TargetType=<span style="color: #006080;">"TextBlock"</span>></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 2:</span> <Setter Property=<span style="color: #006080;">"FontFamily"</span> Value=<span style="color: #006080;">"Verdana"</span> /></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 3:</span> <Setter Property=<span style="color: #006080;">"FontSize"</span> Value=<span style="color: #006080;">"14"</span> /></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 4:</span> </Style></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 5:</span> <Style x:Name=<span style="color: #006080;">"ImportantHeadline"</span> TargetType=<span style="color: #006080;">"TextBlock"</span> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 6:</span> BasedOn=<span style="color: #006080;">"{StaticResource Headline}"</span>></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 7:</span> <Setter Property=<span style="color: #006080;">"Foreground"</span> Value=<span style="color: #006080;">"Red"</span> /></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 8:</span> </Style> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 9:</span> <Style x:Name=<span style="color: #006080;">"HomePageHeadline"</span> TargetType=<span style="color: #006080;">"TextBlock"</span> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 10:</span> BasedOn=<span style="color: #006080;">"{StaticResource Headline}"</span>></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 11:</span> <Setter Property=<span style="color: #006080;">"FontSize"</span> Value=<span style="color: #006080;">"18"</span> /></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 12:</span> </Style></pre> </div> </div> <p>Here we have a base style Headline that targets TextBlock elements and defines values for FontFamily and FontSize. Next we have another style ImportantHeadline which again targets TextBlock elements. Note here the additional attribute BasedOn(Line 6). By using it we are basing this style on the style defined by its value – Headline in this example. Here we set the Foreground property of the TextBlock to be Red.</p> <p>When we use the ImportantHeadline style on a TextBlock we are going to have both of the values set in the Headline style(FontFamily, FontSize) and those set in ImportantHeadline(Foreground). </p> <p>Then another style HomePageHeadline is defined. It is also based on the Headline style, but this time it overrides the base value set for FontSize. Now when we use the HomePageHeadline on a TextBlock we can see that it appears  with FontFamily Verdana and FontSize 18.</p> <p>The results are as follows:</p> <div style="width: 450px; height: 70px;"><object width="100%" height="100%" data="data:application/x-silverlight-2," type="application/x-silverlight-2"> <param name="source" value="http://www.silverlightshow.net/showcase/BasedOnStyles.xap"/> <param name="onerror" value="onSilverlightError"/> <param name="background" value="white"/> <param name="minRuntimeVersion" value="3.0.40307.0"/> <param name="autoUpgrade" value="true"/> <a href="http://go.microsoft.com/fwlink/?LinkID=141205" style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none;" height="65"> </a> </object><iframe style="width: 0pt; height: 0pt; visibility: hidden;border: 0px;"></iframe></div> <p><a href="http://www.silverlightshow.net/showcase/BasedOnStyles.zip">Download Source Code</a></p> <p>Note: you can cascade styles on more than one level. Following the example above you can base another style on the HomePageHeadline and you will have 3 levels of cascading.</p> <p>Hope that helps!</p> <p>That's it!</p> editorial@silverlightshow.net ( Emil Stoychev) http://www.silverlightshow.net/tips/Tip-Cascading-Styles-in-Silverlight-3.aspx#comments http://www.silverlightshow.net/tips/Tip-Cascading-Styles-in-Silverlight-3.aspx Mon, 23 Mar 2009 10:23:00 +0200 Tip: Detecting Network Change in Silverllight 3 Application http://www.silverlightshow.net/tips/Detecting-Network-Change-in-Silverllight-3-Application.aspx <p>In <em>Silverlight 3</em> you are able to check if an internet connection is present. You can also detect network changes.<br /> <br /> See also:<br /> <a href="http://www.silverlightshow.net/items/Silverlight-3-as-a-Desktop-Application-Out-of-Browser-Applications.aspx">Silverlight 3 as a Desktop Application (Out-of-Browser Applications)</a></p> <p><strong>Network <strong>availability</strong> checking:<br /> <br /> </strong></p> <div style="line-height: 100% !important; background-color: white; font-family: courier new; font-size: 11px; overflow: auto;border: #7f9db9 1px solid;"> <table style="border-bottom: #eee 0px solid; border-right-width: 0px; background-color: #fff; margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px; undefined: ;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: #f7f7f7 1px solid; padding-left: 10px; font-family: courier new; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px;"></span><span style="color: #0000ff;">if</span><span style="font-size: 11px;"> (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())  </span></td> </tr> <tr> <td style="background-color: #f7f7f7; undefined: ;">{  </td> </tr> <tr> <td>    <span style="color: #0000ff;">this</span><span style="font-size: 11px;">.InitTasks();  </span></td> </tr> <tr> <td style="background-color: #f7f7f7; undefined: ;">} </td> </tr> </tbody> </table> </div> <p><strong>Network change detection:</strong></p> <div style="line-height: 100% !important; background-color: white; font-family: courier new; font-size: 11px; overflow: auto;border: #7f9db9 1px solid;"> <table style="border-bottom: #eee 0px solid; border-right-width: 0px; background-color: #fff; margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px; undefined: ;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: #f7f7f7 1px solid; padding-left: 10px; font-family: courier new; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px;">NetworkChange.NetworkAddressChanged += </span><span style="color: #0000ff;">new</span><span style="font-size: 11px;"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7; undefined: ;">NetworkAddressChangedEventHandler(NetworkChangedCallback); </td> </tr> </tbody> </table> </div> <p> </p> <div style="line-height: 100% !important; background-color: white; font-family: courier new; font-size: 11px; overflow: auto;border: #7f9db9 1px solid;"> <table style="border-bottom: #eee 0px solid; border-right-width: 0px; background-color: #fff; margin: 2px 0px; width: 99%; border-collapse: collapse; border-top-width: 0px; border-left-width: 0px; undefined: ;" cellspacing="0" cellpadding="0"> <colgroup><col style="border-bottom: #f7f7f7 1px solid; padding-left: 10px; font-family: courier new; white-space: nowrap; font-size: 11px;" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px;"></span><span style="color: #0000ff;">private</span><span style="font-size: 11px;"> </span><span style="color: #0000ff;">void</span><span style="font-size: 11px;"> NetworkChangedCallback(</span><span style="color: #0000ff;">object</span><span style="font-size: 11px;"> sender, EventArgs e)  </span></td> </tr> <tr> <td style="background-color: #f7f7f7; undefined: ;">{  </td> </tr> <tr> <td>    <span style="color: #0000ff;">if</span><span style="font-size: 11px;"> (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())  </span></td> </tr> <tr> <td style="background-color: #f7f7f7; undefined: ;">    {  </td> </tr> <tr> <td>        <span style="color: #0000ff;">this</span><span style="font-size: 11px;">.InitTasks();  </span></td> </tr> <tr> <td style="background-color: #f7f7f7; undefined: ;">    }  </td> </tr> <tr> <td>} </td> </tr> </tbody> </table> </div> <p><strong>Demo</strong><br /> <br /> As an example I created the following demo:</p> <div id="silverlightControlHostTaskList" style="width: 500px; height: 300px;"><object width="100%" height="100%" data="data:application/x-silverlight-2," type="application/x-silverlight-2"> <param name="source" value="http://www.silverlightshow.net/showcase/TaskListService/TaskList.xap"/> <param name="minRuntimeVersion" value="3.0.40624.0" /> <param name="autoUpgrade" value="true" /> <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/> </a> </object></div> <br /> This is a simple tasks list. You can add or delete tasks. The tasks are saved on the server and the application is using WCF Service to connect to the server. Nothing special. But if you save the application for offline use you can use it even if you don't have internet connection. I'm using the <em>isolated storage</em> to keep copy of the data on the client and the application is working with the local copy when there is no connection. It synchronizes automatically whenever the connection comes back. <br /> <p><a href="http://www.silverlightshow.net/showcase/TaskList/TaskList.zip" target="_blank">Download Source</a> </p> <p>That's it!</p> editorial@silverlightshow.net ( Nikolay Raychev) http://www.silverlightshow.net/tips/Detecting-Network-Change-in-Silverllight-3-Application.aspx#comments http://www.silverlightshow.net/tips/Detecting-Network-Change-in-Silverllight-3-Application.aspx Thu, 19 Mar 2009 11:15:00 +0200 Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control? http://www.silverlightshow.net/tips/Tip-How-to-handle-the-MouseLeftButtonDown-and-MouseLeftButtonUp-events-of-the-Button-control.aspx <p>Have you ever noticed that the <b><i>MouseLeftButtonDown</i></b> and <b><i>MouseLeftButtonUp</i></b> events are not fired when a Silverlight button is clicked? The reason for this is that the button handles these two events itself by overriding the <b><i>OnMouseLeftButtonDown</i></b>  and the <b><i>OnMouseLeftButtonUp</i></b>  handlers. In the <b><i>OnMouseLeftButtonDown</i></b>  override, the <b><i>Click</i></b> event is raised and the <b><i>MouseLeftButtonDown</i></b> event is marked as handled so it couldn't bubble in the visual tree. The <b><i>OnMouseLeftButtonUp</i></b>  override also marks the <b><i>MouseLeftButtonUp</i></b>  as handled.</p> <p>This thing can be changed using the <b><i>ClickMode</i></b> property of the Button control. It has the following values - <i>Hover, Press, Release</i>. The default one is <i>Pressed</i> and we have already explained it. When we have ClickMode set to <i>Release</i>, the <b><i>Click</i></b> event will be raised in the <b><i>OnMouseLeftButtonUp</i></b> override and the <b><i>MouseLeftButtonDown</i></b> and <b><i>MouseLeftButtonUp</i></b> events will be handled inside the button again. If we set the <b><i>ClickMode</i></b> to <i>Hover</i>, the <b><i>Click</i></b> event will be raised with the <b><i>MouseEnter</i></b> event and we will also be able to use the mouse button events.</p> <p>That's it!</p> editorial@silverlightshow.net ( Martin Mihaylov) http://www.silverlightshow.net/tips/Tip-How-to-handle-the-MouseLeftButtonDown-and-MouseLeftButtonUp-events-of-the-Button-control.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-handle-the-MouseLeftButtonDown-and-MouseLeftButtonUp-events-of-the-Button-control.aspx Fri, 28 Nov 2008 07:45:00 +0200 Tip: Avoid parameters without values in the object element http://www.silverlightshow.net/tips/Tip-Avoid-parameters-without-values-in-the-object-element.aspx <p>When using the object element to display our Silverlight application, we can use parameters to configure it (source, onerror, background, minRuntimeVersion and etc.):</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">object</span> <span style="color: #ff0000;">data</span><span style="color: #0000ff;">="data:application/x-silverlight-2,"</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="application/x-silverlight-2"</span> <span style="color: #ff0000;">width</span><span style="color: #0000ff;">="100%"</span> <span style="color: #ff0000;">height</span><span style="color: #0000ff;">="100%"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">param</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="source"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="ClientBin/Issues.xap"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">param</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="onerror"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="onSilverlightError"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">param</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="background"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="white"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">param</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="minRuntimeVersion"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="2.0.31005.0"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">param</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="autoUpgrade"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">a</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="http://go.microsoft.com/fwlink/?LinkID=124807"</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="text-decoration: none;"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">img</span> <span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://go.microsoft.com/fwlink/?LinkId=108181"</span> <span style="color: #ff0000;">alt</span><span style="color: #0000ff;">="Get Microsoft Silverlight"</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="border-style: none"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">object</span><span style="color: #0000ff;">></span></pre> </div> </div> <p>Some of them are predefined in the automatically created TestPages, but we can also add additional parameters like initParams, onload and etc.</p> <p>Be sure <b><i>always</i></b> to set the value of each parameter, otherwise browsers different than Internet Explorer will not be able to render the object element properly and the "Install Silverlight" badge will appear instead of the Silverlight application. This issue could be a showstopper when embedding Silverlight applications into a web page and is not related to the Silverlight plug-in.</p> <p> </p> <p>That's it!</p> editorial@silverlightshow.net ( Martin Mihaylov) http://www.silverlightshow.net/tips/Tip-Avoid-parameters-without-values-in-the-object-element.aspx#comments http://www.silverlightshow.net/tips/Tip-Avoid-parameters-without-values-in-the-object-element.aspx Fri, 28 Nov 2008 07:37:00 +0200 Tip: What is the difference between styling and skinning http://www.silverlightshow.net/tips/Tip-What-is-the-difference-between-styling-and-skinning.aspx <div><a href="http://www.silverlightshow.net/tips/What-is-control-styling.aspx">Styling</a> control means to apply to the control's properties predefined in the style values. Styling deals mainly with properties like <em>Foreground</em>, <em>Background</em>, <em>FontStyle </em>and <em>FontSize</em>, etc.</div> <div> </div> <div>Skinning, on the other hand, allows you to build your own presentation for that control by defining control template. Thus, you are able to completely replace the standard presentation with another, which includes more visual elements, animations and other effects.</div> <div> </div> <div>That's it!</div> editorial@silverlightshow.net ( Ivan Dragoev) http://www.silverlightshow.net/tips/Tip-What-is-the-difference-between-styling-and-skinning.aspx#comments http://www.silverlightshow.net/tips/Tip-What-is-the-difference-between-styling-and-skinning.aspx Fri, 14 Nov 2008 06:04:00 +0200 Tip: What is control skinning? http://www.silverlightshow.net/tips/Tip-What-is-control-skinning.aspx <p>Skinning is a way to completely replace the look of your control. In order to do that, you have to define new template and set it to the <em>Control.Template </em>property.</p> <p>Building your own template allows you to make the control look and feel in the way you like. You might add new elements or remove the existing, you can also edit the visual states and transitions.</p> <p>In the example the button shape is changed. All animations are removed for simplicity.</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl.Resources</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="myButtonTemplate"</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Template"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 6:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="BackgroundGradient"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="#FF94C2D8"</span> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 8:</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="1"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 9:</span> <span style="color: #ff0000;">Data</span><span style="color: #0000ff;">="M44,-135 L137,-136 L163.00137,-114.43846 L138.14738,-98.33696 L40.513062,-96.888382 L65.010727,-116.44418 z" ></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 10:</span> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path.Fill</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 12:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearGradientBrush</span> <span style="color: #ff0000;">EndPoint</span><span style="color: #0000ff;">="0.5,1"</span> <span style="color: #ff0000;">StartPoint</span><span style="color: #0000ff;">="0.5,0"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 13:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFA3AEB9"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 14:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FF8399A9"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 15:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FF718597"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 16:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FF617584"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 17:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearGradientBrush</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 18:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Path.Fill</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 19:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Path</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 20:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 21:</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="contentPresenter"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 22:</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="{TemplateBinding Content}"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 23:</span> <span style="color: #ff0000;">ContentTemplate</span><span style="color: #0000ff;">="{TemplateBinding ContentTemplate}"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 24:</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="{TemplateBinding VerticalContentAlignment}"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 25:</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="{TemplateBinding HorizontalContentAlignment}"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 26:</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="{TemplateBinding Padding}"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 27:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 28:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 29:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 30:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 31:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 32:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">UserControl.Resources</span><span style="color: #0000ff;">></span></pre> </div> </div> <p>That's it!</p> editorial@silverlightshow.net ( Ivan Dragoev) http://www.silverlightshow.net/tips/Tip-What-is-control-skinning.aspx#comments http://www.silverlightshow.net/tips/Tip-What-is-control-skinning.aspx Fri, 14 Nov 2008 05:40:00 +0200 Tip: What is control styling? http://www.silverlightshow.net/tips/What-is-control-styling.aspx <p>Styling control is a very easy way to tweak the minor visual characteristic of the control such as foreground and background colors, the font style and size, padding, etc. Usually the styles are stored as resources which allows you to reuse them whenever you want.</p> <p>The Style has TargetType specifying the type of the object the style can be applied to and a key used to identify the style. The Style contains numerous Setters, which describe the values which you want to set to specific properties.</p> <p>To apply style to a control simply specify the Style property of the control to point to the static resource with a specific key name.</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.Resources</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="myButtonStyle"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Width"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="100"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Height"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="25"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 6:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Foreground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="#FF87091F"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Padding"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="2"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 8:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="BorderThickness"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="4"</span><span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 9:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontWeight"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Bold"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 10:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontFamily"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Portable User Interface"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 11:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 12:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.Resources</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 13:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Click Me!"</span> <span style="color: #ff0000;">Style</span><span style="color: #0000ff;">="{StaticResource myButtonStyle}"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 14:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Button</span><span style="color: #0000ff;">></span> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> 15:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> </div> </div> <p> That's it!</p> editorial@silverlightshow.net ( Ivan Dragoev) http://www.silverlightshow.net/tips/What-is-control-styling.aspx#comments http://www.silverlightshow.net/tips/What-is-control-styling.aspx Fri, 14 Nov 2008 02:52:00 +0200 Tip: How to capture mouse scroll / wheel event in Silverlight? http://www.silverlightshow.net/tips/Tip-How-to-capture-mouse-scroll-wheel-event-in-Silverlight.aspx <p>Silverlight doesn't support mouse wheel event. There is a way to catch them if we ask to the browser for them. This workaround works well if the page with your Silverlight application fits in the browser. If the page is bigger in height a scroll appears. Then the whole content of the page is scrolled and the SL application doesn't receive the wheel event until one end is reached.</p> <p>Hook up to the event. Different names are used in the different browsers.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">using</span> System.Windows.Browser;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">...</pre> </div> </div> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">HtmlPage.Window.AttachEvent( <span style="color: #006080;">"DOMMouseScroll"</span>, OnMouseWheel ); // Mozilla</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">HtmlPage.Window.AttachEvent( <span style="color: #006080;">"onmousewheel"</span>, OnMouseWheel );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">HtmlPage.Document.AttachEvent( <span style="color: #006080;">"onmousewheel"</span>, OnMouseWheel ); // IE</pre> </div> </div> <p>This is how you get the wheel delta.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> OnMouseWheel( <span style="color: #0000ff;">object</span> sender, HtmlEventArgs args )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">double</span> mouseDelta = 0;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> ScriptObject e = args.EventObject;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">// Mozilla and Safari</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">if</span> ( e.GetProperty( <span style="color: #006080;">"detail"</span> ) != <span style="color: #0000ff;">null</span> )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> mouseDelta = ( ( <span style="color: #0000ff;">double</span> )e.GetProperty( <span style="color: #006080;">"detail"</span> ) );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">// IE and Opera</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ( e.GetProperty( <span style="color: #006080;">"wheelDelta"</span> ) != <span style="color: #0000ff;">null</span> )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> mouseDelta = ( ( <span style="color: #0000ff;">double</span> )e.GetProperty( <span style="color: #006080;">"wheelDelta"</span> ) );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> mouseDelta = Math.Sign( mouseDelta );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> </div> </div> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/Tip-How-to-capture-mouse-scroll-wheel-event-in-Silverlight.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-capture-mouse-scroll-wheel-event-in-Silverlight.aspx Thu, 18 Sep 2008 08:51:00 +0200 Tip: What are the differences in the styles system between Silverlight and WPF? http://www.silverlightshow.net/tips/Tip-What-are-the-differences-in-the-styles-system-between-Silverlight-and-WPF.aspx <p>These are the differences:</p> <ul> <li>Is it possible to apply a style to all elements in your application? </li> </ul> <blockquote> <p>Unforunately, the Beta 2 does not support implicit styles using the <em>TargetType</em> attribute like WPF. To apply a style to all elements you must explicitly set a value to each <em>Style</em> property.</p> </blockquote> <ul> <li>Is it possible to extend a style? </li> </ul> <blockquote> <p>In Silverlight styles cannot be based on other styles. In WPF that is possible using the <em>BasedOn</em> attribute</p> </blockquote> <ul> <li>Is it possible to change the style of a control more than once? </li> </ul> <blockquote> <p>No. Styles can be set only once in Silverlight in XAML or in code. After applying a style it is possible to change individual control properties though. You can affect the appearance of a control by changing the Template property and you can do that as many times as you want.</p> </blockquote> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/Tip-What-are-the-differences-in-the-styles-system-between-Silverlight-and-WPF.aspx#comments http://www.silverlightshow.net/tips/Tip-What-are-the-differences-in-the-styles-system-between-Silverlight-and-WPF.aspx Thu, 18 Sep 2008 08:43:00 +0200 Tip: How to find the styles and templates of a standard control? http://www.silverlightshow.net/tips/Tip-How-to-find-the-styles-and-templates-of-a-standard-control.aspx <p>Changing the default style or template of a control from the standard library is the same as with any other control. Except to create your own style it is good to know the parts and the states that compose the standard style. Microsoft has released the full XAML of a number of controls along with description of their visual states and state groups - <a href="http://msdn.microsoft.com/en-us/library/cc278075(VS.95).aspx">Control Styles and Templates</a>. Additionally using the reflector you can extract the styles of some slightly different controls, we did that and uploaded the <a href="http://www.silverlightshow.net/storage/userfiles/generic.zip">generic.xaml</a> file for you. Lastly, in the Reflector you can see the states and parts of every control  listed on the first line of the page. See how to disassemble Silverlight assemblies <a href="http://www.silverlightshow.net/tips/How-to-disassemble-the-source-code-of-the-Silverlight-assemblies.aspx" target="_blank">here</a>.</p> <p>  <img alt="" src="file:///C:/Documents%20and%20Settings/Dimitar%20Savkov/Desktop/TemplateParts.png" /><img alt="" width="572" height="189" src="http://www.silverlightshow.net/storage/userfiles/TemplateParts.png" /></p> <p>ListBox has only one visual part - ScrollViewer.</p> <p> <img alt="" width="925" height="288" src="http://www.silverlightshow.net/storage/userfiles/TemplateVisualStates.png" /></p> <p>Here you see two of the six Button visual states.</p> <p> That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/Tip-How-to-find-the-styles-and-templates-of-a-standard-control.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-find-the-styles-and-templates-of-a-standard-control.aspx Thu, 18 Sep 2008 08:41:00 +0200 Tip: How to enter full-screen mode? http://www.silverlightshow.net/tips/Tip-How-to-enter-full-screen-mode.aspx <p>Just use the following line.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">Application.Current.Host.Content.IsFullScreen = <span style="color: #0000ff;">true</span>; </pre> </div> </div> <p>Entering full-screen mode in Silverlight has some restrictions. To ensure that it is initiated by the user entering full-screen mode is possible only in response to one of these input events: MouseLeftButtonDown. MouseLeftButtonUp, KeyDown, KeyUp.</p> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/Tip-How-to-enter-full-screen-mode.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-enter-full-screen-mode.aspx Thu, 18 Sep 2008 08:28:00 +0200 Tip: How to validate values against certain rules when using binding? http://www.silverlightshow.net/tips/Tip-How-to-validate-values-against-certain-rules-when-using-binding.aspx <p>Typically validation occurs when in a two-way binding the business object (source property) is updated with data from the user input (target property). During the update there are two places where validation occurs.</p> <ol> <li>First, the binding engine uses a implementation of <em>IValueConverter </em>to convert the data from one type to the other. Usually this converter has some validation. Unfortunately, Silverlight doesn't provide a way for custom validation for binding, like in WPF. There is no <em>ValidationRule</em> class that is used in WPF to provide the custom rule for the validation. </li> <li>Second, the setter of the property being updated may contain validation. </li> </ol> <p>This means that if you want custom validation you have to write your own type converters or to place the validation inside the setters of your business object. In case the validation fails, an exception is thrown in a setter or in a converter , then a <em>BindingValidationError</em> event is raised (it is member of <em>System.Windows.Controls.FrameworkElement</em>). This makes it easy for you to prompt the user to enter correct data. <em>BindingValidationError</em> can be raised only when both <em>NotifyOnValidationError</em> and <em>ValidatesOnExceptions</em> are true. Setting ValidatesOnExceptions to true tells the binding engine to report validation exceptions and setting NotifyOnValidationError to true tells the binding engine to raise the BindingValidationError event when a validation error occurs. This, combined with the <em>TwoWay</em> mode of binding, gives us the following:</p> <p><strong>XAML</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">TextBox</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="bindingTarget"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Path=Age,</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> Source={StaticResource person},</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> Mode=TwoWay,</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> NotifyOnValidationError=true,</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> ValidatesOnExceptions=true}" <span style="color: #0000ff;">/></span>. </pre> </div> </div> <div><strong>C#</strong></div> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">bindingTarget.BindingValidationError +=</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> EventHandler<ValidationErrorEventArgs>( bindingTarget_BindingValidationError );</pre> </div> </div> <p>Here <em>Age</em> is a property of the object <em>person</em>.</p> <p>Strangely <em>BindingValidationError</em> won’t raise if you use custom type converters in your binding. Check out our demo and <a href="http://www.silverlightshow.net/storage/userfiles/ValidationTest_RTW.zip">download the source</a>.</p> <div id="silverlightControlHost"><object width="300" height="400" data="data:application/x-silverlight," type="application/x-silverlight-2"> <param value="http://www.silverlightshow.net/showcase/ValidationTest_RTW.xap" name="source" /> <param value="onSilverlightError" name="onerror" /> <param value="white" name="background" /> <param value="2.0.31005.0" name="minRuntimeVersion" /> <param value="true" name="autoUpgrade" /> <a style="text-decoration: none;" href="http://go.microsoft.com/fwlink/?LinkID=124807"> <img style="border-style: none;" alt="Get Microsoft Silverlight" src="http://go.microsoft.com/fwlink/?LinkId=108181" /> </a> </object><iframe style="width: 0pt; height: 0pt; visibility: hidden;border: 0px;"></iframe></div> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/Tip-How-to-validate-values-against-certain-rules-when-using-binding.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-validate-values-against-certain-rules-when-using-binding.aspx Wed, 17 Sep 2008 01:51:00 +0200 Tip: How to get reference to the object that raised a routed event? http://www.silverlightshow.net/tips/How-to-get-reference-to-the-object-that-raised-a-routed-event.aspx <p>The callback function of a routed event has the second parameter <em>e</em> of type <em>RoutedEventArgs</em> or its inheritor. The <em>Source</em> property of this parameter holds reference to the object that raised the event. In contrast to that the sender parameter holds reference to the object where the event handler was invoked.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">void</span> RoutedEventRaised( <span style="color: #0000ff;">object</span> sender, RoutedEventArgs e )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">object</span> source = e.Source;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> </div> </div> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/How-to-get-reference-to-the-object-that-raised-a-routed-event.aspx#comments http://www.silverlightshow.net/tips/How-to-get-reference-to-the-object-that-raised-a-routed-event.aspx Wed, 17 Sep 2008 01:09:00 +0200 Tip: How to shorten your C# code? http://www.silverlightshow.net/tips/How-to-shorten-your-C-code.aspx <p>Some of the new features in C# 3.0 allow you to write shorter code that is easier to read and maintain. The following code couples have the same results.</p> <ul> <li>Short syntax for <strong>object</strong> initialization initialization. <p><strong>C#</strong></p> <p> </p> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">ObjectType objectName = <span style="color: #0000ff;">new</span> ObjectType();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">objectName.PropertyName = newValue;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">objectName.PropertyName2 = newValue2;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> </div> <p> </p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">ObjectType objectName = <span style="color: #0000ff;">new</span> ObjectType(){ PropertyName = newValue, PropertyName2 = newValue2 };</pre> </div> </div> </li> </ul> <ul> <li>Syntaxt for <strong>anonymous types</strong> </li> </ul> <blockquote> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">SomeVeryLongNameOfType objectName = <span style="color: #0000ff;">new</span> SomeVeryLongNameOfType();</pre> </div> </div> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">var objectName = SomeVeryLongNameOfType();</pre> </div> </div> </blockquote> <ul> <li>Short syntax for <strong>collection</strong> initialization </li> </ul> <blockquote> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">CollectionType collectionName = <span style="color: #0000ff;">new</span> CollectionType();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">collectionName.Add( newItem1 );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">..</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">collectionName.Add( newItemN );</pre> </div> </div> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">CollectionType collectionName = <span style="color: #0000ff;">new</span> CollectionType() { Item1,...,ItemN };</pre> </div> </div> </blockquote> <p> </p> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/How-to-shorten-your-C-code.aspx#comments http://www.silverlightshow.net/tips/How-to-shorten-your-C-code.aspx Wed, 17 Sep 2008 01:05:00 +0200 Tip: How to disable controls in Silverlight? http://www.silverlightshow.net/tips/Tip-How-to-disable-controls-in-Silverlight.aspx <p>Currently only Silverlight controls that derive from <em>System.Windows.Controls.ContentControl</em> support disabling. Those that support have <em>IsEnabled</em> property. However in the release version of Silverlight 2.0 <em>IsEnabled </em>will be a property of the <em>System.Windows.Control</em> class. Also, if the property is changed on a parent control the new value will be applied to all the children of the control.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">myButton.IsEnabled = <span style="color: #0000ff;">false</span>;</pre> </div> </div> <p><strong>XAML</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">IsEnabled</span><span style="color: #0000ff;">="False"</span><span style="color: #0000ff;">/></span></pre> </div> </div> <p>If you need to disable your controls before the release you must write your own implementation. It is useful to set the <em>UIElement.IsHitTestVisible</em> property to false. It prevents the <em>UIElement</em> from raising input events and receiving focus, and is applied to the children elements.</p> <p><strong>XAML</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="False"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">...</span><span style="color: #0000ff;">><!</span><span style="color: #800000;">--IsHitTestVisible</span> <span style="color: #ff0000;">property</span> <span style="color: #ff0000;">of</span> <span style="color: #ff0000;">items</span> <span style="color: #ff0000;">is</span> <span style="color: #ff0000;">forced</span> <span style="color: #ff0000;">to</span> <span style="color: #ff0000;">False--</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> </div> </div> <p>That's it!</p> editorial@silverlightshow.net ( Denislav Savkov) http://www.silverlightshow.net/tips/Tip-How-to-disable-controls-in-Silverlight.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-disable-controls-in-Silverlight.aspx Wed, 17 Sep 2008 00:59:00 +0200 Tip: How to add a tooltip to a control? http://www.silverlightshow.net/tips/Tip-How-to-add-a-tooltip-to-a-control.aspx <p>If you want to add a tooltip to a control you must first check if it allows the ToolTipService namespace. If yes, you can add the tooltip the following way:</p> <p><strong>Xaml</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="MyButton"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Hello!"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="50"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="30"</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ToolTipService.ToolTip</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="ToolTip"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="Click me!"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ToolTipService.ToolTip</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Button</span><span style="color: #0000ff;">></span></pre> </div> </div> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">ToolTipService.SetToolTip( MyButton, <span style="color: #0000ff;">new</span> TextBlock() { Text = <span style="color: #006080;">"Click me!"</span> } );</pre> </div> </div> <p>You can also use another control instead of TextBlock, for example Image control or Rectangle control.</p> <p>That's it!<br />  </p> editorial@silverlightshow.net ( Martin Mihaylov) http://www.silverlightshow.net/tips/Tip-How-to-add-a-tooltip-to-a-control.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-add-a-tooltip-to-a-control.aspx Thu, 11 Sep 2008 07:24:00 +0200 Tip: How to declare an attached property in Silverlight? http://www.silverlightshow.net/tips/Tip-How-to-declare-an-attached-property-in-Silverlight.aspx <p>Probably the first most important thing to mention here is that only inheritors of <em>System.Windows.DependencyObject</em> can be extended with attached properties. This is needed because either to set or to get value for a specific attached property, you need to use the methods <em>SetValue</em> and <em>GetValue</em> which are defined in the <em>DependencyObject</em> class. <br /> When you declare an attached property, many advantages from the dependency properties model are coming out of the box for you such as caching, data binding, default values, expressions, styling, property invalidation and more. <strong><em><br /> </em></strong></p> <p>In order to declare a attached property in Silverlight, you have to follow few simple steps as explained in the sample code below.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> MySilverlightControl : StackPanel</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;"><strong>//1. Declare the attached property as static, readonly field in your class.</strong></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached(</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #006080;">"MyProperty"</span>, <span style="color: #008000;">//Property name</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">typeof</span>( <span style="color: #0000ff;">string</span> ), <span style="color: #008000;">//Property type</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">typeof</span>( MySilverlightControl ), <span style="color: #008000;">//Type of the dependency property provider</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> PropertyMetadata( MyPropertyChanged ) )<span style="color: #008000;">//Callback invoked on property value change</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> MySilverlightControl()</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> InitializeComponent();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;"><strong>//2. Define the appropriate SetXXX and GetXXX methods, </strong></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;"><strong>//where XXX should be replaced with the property name</strong></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> SetMyProperty( DependencyObject obj, <span style="color: #0000ff;">string</span> propertyValue )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> obj.SetValue( MyPropertyProperty, propertyValue );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span> GetMyProperty( DependencyObject obj )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> ( <span style="color: #0000ff;">string</span> )obj.GetValue( MyPropertyProperty );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> MyPropertyChanged( <span style="color: #0000ff;">object</span> sender, DependencyPropertyChangedEventArgs args )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">//Do some processing here when the attached property value has changed...</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">//Just a sample method illustrating the idea how to obtain the value</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">//of the MyProperty property for a specific element.</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> ProcessTabKey()</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">foreach</span> ( UIElement element <span style="color: #0000ff;">in</span> <span style="color: #0000ff;">this</span>.Children )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><strong> <span style="color: #0000ff;">string</span> propertyValue = MySilverlightControl.GetMyProperty( element );</strong></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">//Perform some processing according to the my property value.............</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> </div> </div> <p>Read more about <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyobject(VS.95).aspx">DependencyObject</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty(VS.95).aspx">DependencyProperty</a> classes on MSDN.<br /> If you need more information about the attached properties check out this article '<a href="http://www.silverlightshow.net/items/Attached-Properties-in-Silverlight.aspx">Attached properties in Silverlight</a>'.</p> <p>That's it!</p> editorial@silverlightshow.net ( Ilia Iordanov) http://www.silverlightshow.net/tips/Tip-How-to-declare-an-attached-property-in-Silverlight.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-declare-an-attached-property-in-Silverlight.aspx Wed, 10 Sep 2008 05:21:00 +0200 Tip: How to declare a dependency property in Silverlight? http://www.silverlightshow.net/tips/Tip-How-to-declare-a-dependancy-property-in-Silverlight.aspx <p>Probably the first most important thing to mention here is that only inheritors of <em>System.Windows.DependencyObject</em> can be extended with dependency properties. This is needed because either to set or to get value for a specific dependency property, you need to use the methods <em>SetValue</em> and <em>GetValue</em> which are defined in the <em>DependencyObject</em> class. <br /> <em>W</em>hen you declare a dependency property, many advantages are coming out of the box for you such as caching, data binding, default values, expressions, styling, property invalidation and more.<em><strong><br /> </strong></em></p> <p>In order to declare a dependency property in Silverlight, you have to follow few simple steps as explained in the sample code below.</p> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> MySilverlightControl : UserControl</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <strong><span style="color: #008000;">//1. Declare the dependency property as static, readonly field in your class.</span></strong></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty MyPropertyProperty = DependencyProperty.Register(</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #006080;">"MyProperty"</span>, <span style="color: #008000;">//Property name</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">typeof</span>( <span style="color: #0000ff;">string</span> ), <span style="color: #008000;">//Property type</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">typeof</span>( MySilverlightControl ), <span style="color: #008000;">//Type of the dependency property provider</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> PropertyMetadata( MyPropertyChanged ) );<span style="color: #008000;">//Callback invoked on property value has changes</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> MySilverlightControl()</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> InitializeComponent();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <strong><span style="color: #008000;">//2. Create the property and use the SetValue/GetValue methods of the DependencyObject class</span></strong></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> MyProperty</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> set</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">this</span>.SetValue( MyPropertyProperty, <span style="color: #0000ff;">value</span> );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> get</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> ( <span style="color: #0000ff;">string</span> )<span style="color: #0000ff;">this</span>.GetValue( MyPropertyProperty );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> MyPropertyChanged( <span style="color: #0000ff;">object</span> sender, DependencyPropertyChangedEventArgs args )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;">//Do some processing here...</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> </div> </div> <p>Read more about <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyobject(VS.95).aspx">DependencyObject</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty(VS.95).aspx">DependencyProperty</a> classes on MSDN.</p> <p>That's it!</p> editorial@silverlightshow.net ( Ilia Iordanov) http://www.silverlightshow.net/tips/Tip-How-to-declare-a-dependancy-property-in-Silverlight.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-declare-a-dependancy-property-in-Silverlight.aspx Wed, 10 Sep 2008 02:19:00 +0200 Tip: How to pass a parameter to a value converter? http://www.silverlightshow.net/tips/Tip-How-to-pass-a-parameter-to-a-value-converter.aspx <p>If you're not familiar with the value converters read <a href="http://www.silverlightshow.net/items/How-to-format-a-binding-value-using-convertors.aspx">this</a>. The methods generated by the VisualStudio when creating a custom class that implements System.Windows.Data.IValueConverter have several arguments. One of them is of type object and is called parameter.</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> DateTimeConverter : System.Windows.Data.IValueConverter</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> Convert( <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>, Type targetType, <strong><span style="color: #0000ff;">object</span> parameter</strong>, System.Globalization.CultureInfo culture )...</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> ConvertBack( <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>, Type targetType, <strong><span style="color: #0000ff;">object</span> parameter</strong>, System.Globalization.CultureInfo culture )...</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> </div> </div> <p>In this example we bind to an object of type Book:</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Book</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> DateTime PublishDate { get; set; }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> </div> </div> <p>We can pass this argument from the code or from the Xaml:</p> <p><strong>Xaml</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding PublishDate, Converter={StaticResource DateTimeConverter}, <strong>ConverterParameter=true</strong>}"</span><span style="color: #0000ff;">/></span></pre> </div> </div> <p><strong>C#</strong></p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">Book myBook = <span style="color: #0000ff;">new</span> Book();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">myBook.PublishDate = DateTime.Now;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">Binding binding = <span style="color: #0000ff;">new</span> Binding( <span style="color: #006080;">"PublishDate"</span> );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">binding.Source = myBook;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">binding.Converter = <span style="color: #0000ff;">new</span> DateTimeConverter();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><strong>binding.ConverterParameter = <span style="color: #0000ff;">true</span>;</strong></pre> </div> </div> <p>That's it!</p> editorial@silverlightshow.net ( Martin Mihaylov) http://www.silverlightshow.net/tips/Tip-How-to-pass-a-parameter-to-a-value-converter.aspx#comments http://www.silverlightshow.net/tips/Tip-How-to-pass-a-parameter-to-a-value-converter.aspx Tue, 09 Sep 2008 08:22:00 +0200 Tip: How to invoke a JavaScript function from the Silverlight? http://www.silverlightshow.net/tips/How-to-invoke-a-JavaScript-function-from-the-Silverlight.aspx <p>In order to invoke a JavaScript function  we must access the DOM from the Silverlight application. This can be done with the help of the System.Windows.Browser.HtmlPage class. Here it is how it's done:</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">HtmlPage.Window.Invoke( <span style="color: #006080;">"Function_Name"</span>, <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[] { //Array of paramaters that must be passed to the function } );</pre> </div> </div> <p>We call the Invoke method of the Window property, which takes the name of the function and an array of its arguments as parameters.</p> <p>That's it!</p> editorial@silverlightshow.net ( Martin Mihaylov) http://www.silverlightshow.net/tips/How-to-invoke-a-JavaScript-function-from-the-Silverlight.aspx#comments http://www.silverlightshow.net/tips/How-to-invoke-a-JavaScript-function-from-the-Silverlight.aspx Tue, 09 Sep 2008 07:50:00 +0200 Tip: How to format a binding value using converters? http://www.silverlightshow.net/tips/How-to-format-a-binding-value-using-convertors.aspx <p>First let's explain what the converters can be used for. Imagine you bind to an object's property, but the property is not formatted to your likings. In this case you can use converters. For example we bind to the PublishDate property of a Book object and want the date to be formatted like this - "dd MMM, yyyy".</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">Book myBook = <span style="color: #0000ff;">new</span> Book();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">myBook.PublishDate = DateTime.Now;</pre> </div> </div> <p>First let's create our converter. It's a class that implements <em>System.Windows.Data.IValueConverter </em>(using VisualStudio you can easily generate the structure of the class).</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> DateTimeConverter : System.Windows.Data.IValueConverter</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> Convert( <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>, Type targetType, <span style="color: #0000ff;">object</span> parameter, System.Globalization.CultureInfo culture )</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> ( ( DateTime )<span style="color: #0000ff;">value</span> ).ToString( <span style="color: #006080;">"dd MMM, yyyy"</span> );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> ConvertBack( <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>, Type targetType, <span style="color: #0000ff;">object</span> parameter, System.Globalization.CultureInfo culture )...</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> </div> </div> <p>We have a method Convert, which takes several parameters. The value parameter will be the value of the property that we want to convert. It's of type object so we cast it to DateTime and use the ToString method of the DateTime class to format it as we please. Let's now see how to use this converter in the Xaml. First declare the namespace of the converter:</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl</span> <span style="color: #ff0000;">x:Class</span><span style="color: #0000ff;">="LayoutExperiments.Page"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #ff0000;"><strong>xmlns:local</strong></span><span><strong>="clr-namespace:LayoutExperiments"</strong>></span></pre> </div> </div> <p>My DateTimeConverter class is defined in the Page.cs file, but you can put anywhere you want. The next step is to add the Resource to the UserControl of type DateTimeConverter:</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl.Resources</span><span style="color: #0000ff;">></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">local:DateTimeConverter</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="DateTimeConverter"</span> <span style="color: #0000ff;">/></span></pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">UserControl.Resources</span><span style="color: #0000ff;">></span></pre> </div> </div> <p>And the last thing is to configure the binding and add the converter to it:</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="MyText"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding PublishDate, Converter={StaticResource DateTimeConverter}}"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span></pre> </div> </div> <p>This can also be done entirely in C# using the System.Windows.Data.Binding class:</p> <div> <div style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">TextBlock myText = <span style="color: #0000ff;">new</span> TextBlock();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">Book myBook = <span style="color: #0000ff;">new</span> Book();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">myBook.PublishDate = DateTime.Now;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">Binding binding = <span style="color: #0000ff;">new</span> Binding(<span style="color: #006080;">"PublishDate"</span>);</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">binding.Source = myBook;</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">binding.Converter = <span style="color: #0000ff;">new</span> DateTimeConverter();</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: rgb(244,244,244); margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">myText.SetBinding( TextBlock.TextProperty, binding );</pre> <pre style="padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas,'courier new',courier,monospace; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">LayoutRoot.Children.Add( myText );</pre> </div> </div> <p>So thanks to this converter the value of the TextBlock will be "09 Sep, 2008" instead of "9/9/2008 3:31:PM".</p> <p>That's it!</p> editorial@silverlightshow.net ( Martin Mihaylov) http://www.silverlightshow.net/tips/How-to-format-a-binding-value-using-convertors.aspx#comments http://www.silverlightshow.net/tips/How-to-format-a-binding-value-using-convertors.aspx Tue, 09 Sep 2008 05:55:00 +0200