SilverlightShow: Creating a Silverlight Custom Control - The Basics Comments 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 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment3824 I am trying to design a basic control, basically a content window with a title. <div>On par with your example I would need a dependancy property "Title". How do I make it so that the actual label inside the control is set/bound to the value of "Title"? Since the label is inside of a template I can't actually reference it and since the DataContext of the control is bound to something else. I don't see you doing anything with your dependancy property above, I am confused.</div> ( Brandon) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Mon, 21 Jun 2010 02:38:57 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment3279 I got the same problem as Anand, further investigation of what causes the error led me to find that the Template is not set for my control. Any help would be welcomed ( C# Student) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Fri, 19 Mar 2010 22:41:50 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment2202 <p>Hi,</p> <p> I am using Silverlight 2 env for my development. I have created a silverlight control library to create some custom controls which i would be using in my application. I was able to create the project sucessfully add a custom control and integrate with another appication to consume the control, that too works good. Now I try to add another custom control to the same project I did the following steps.</p> <p>1. Created a cs file for the second custom control.</p> <p>2. in generic.xaml I added another style tag to the rescource dictionary for the new custom control.</p> <p>3. did all the coding part in cs file required and complied the app.</p> <p>4. referenced the app to another project to consume it. Now i see the custom control 1 and as well the custom control 2 have runtime error(its just the AG_E_UNKOWN_Error). </p> <p>But without the style block for the second control in the resource dictionary my custom control 1 works fine if i add the style for the custom control 2. It cause the runtime error.</p> <p>Could any pls correct me if am not using resourcedictionary properly or is there anything else Iam missing to have a library containing more than one custom control in it.</p> <p><span style="color: #0000ff; font-size: 13px;"><</span><span style="color: #a31515; font-size: 13px;">ResourceDictionary<br /> </span><span style="color: #ff0000; font-size: 13px;">xmlns</span><span style="color: #0000ff; font-size: 13px;">=http://schemas.microsoft.com/winfx/2006/xaml/presentation<br /> </span><span style="color: #ff0000; font-size: 13px;">xmlns</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #ff0000; font-size: 13px;">x</span><span style="color: #0000ff; font-size: 13px;">=<a href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a></span><span style="color: #ff0000; font-size: 13px;"><br /> xmlns</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #ff0000; font-size: 13px;">sys</span><span style="color: #0000ff; font-size: 13px;">="clr-namespace:System.Windows;assembly=System.Windows"<br /> </span><span style="color: #ff0000; font-size: 13px;">xmlns</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #ff0000; font-size: 13px;">xyzCtl</span><span style="color: #0000ff; font-size: 13px;">="clr-namespace:xyzCtlLibrary"<br /> </span><span style="color: #ff0000; font-size: 13px;">xmlns</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #ff0000; font-size: 13px;">vsm</span><span style="color: #0000ff; font-size: 13px;">="clr-namespace:System.Windows;assembly=System.Windows"</span><span style="font-size: 13px;"> </span><span style="color: #0000ff; font-size: 13px;">></span><span style="color: #a31515; font-size: 13px;"> <br /> </span><span style="color: #0000ff; font-size: 13px;"> <</span><span style="color: #a31515; font-size: 13px;">Style</span><span style="color: #ff0000; font-size: 13px;"> TargetType</span><span style="color: #0000ff; font-size: 13px;">="xyzCtl:Expander"><br />   </span><span style="color: #0000ff; font-size: 13px;"><</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #ff0000; font-size: 13px;"> Property</span><span style="color: #0000ff; font-size: 13px;">="BorderThickness"</span><span style="color: #ff0000; font-size: 13px;"> Value</span><span style="color: #0000ff; font-size: 13px;">="1"/><br /> </span><span style="color: #0000ff; font-size: 13px;">  <</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #ff0000; font-size: 13px;"> Property</span><span style="color: #0000ff; font-size: 13px;">="HorizontalContentAlignment"</span><span style="color: #ff0000; font-size: 13px;"> Value</span><span style="color: #0000ff; font-size: 13px;">="Stretch"/><br /> </span><span style="color: #0000ff; font-size: 13px;">  <</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #ff0000; font-size: 13px;"> Property</span><span style="color: #0000ff; font-size: 13px;">="VerticalContentAlignment"</span><span style="color: #ff0000; font-size: 13px;"> Value</span><span style="color: #0000ff; font-size: 13px;">="Stretch"/><br /> </span><span style="color: #0000ff; font-size: 13px;">  <</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #ff0000; font-size: 13px;"> Property</span><span style="color: #0000ff; font-size: 13px;">="Template"><br /> </span><span style="color: #0000ff; font-size: 13px;">   <</span><span style="color: #a31515; font-size: 13px;">Setter.Value</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;">    <</span><span style="color: #a31515; font-size: 13px;">ControlTemplate</span><span style="color: #ff0000; font-size: 13px;"> TargetType</span><span style="color: #0000ff; font-size: 13px;">="xyzCtl:Expander"><br /> </span><span style="color: #0000ff; font-size: 13px;">    <!--  Control Template XAML--!><br /> </span><span style="color: #0000ff; font-size: 13px;">    </</span><span style="color: #a31515; font-size: 13px;">ControlTemplate</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;">   </</span><span style="color: #a31515; font-size: 13px;">Setter.Value</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;">  </</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;"> </</span><span style="color: #a31515; font-size: 13px;">Style</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;"> <</span><span style="color: #a31515; font-size: 13px;">Style</span><span style="color: #ff0000; font-size: 13px;"> TargetType</span><span style="color: #0000ff; font-size: 13px;">="xyzCtlLibrary:Widgets"><br /> </span><span style="color: #0000ff; font-size: 13px;">  <</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #ff0000; font-size: 13px;"> Property</span><span style="color: #0000ff; font-size: 13px;">="Template"><br /> </span><span style="color: #0000ff; font-size: 13px;">   <</span><span style="color: #a31515; font-size: 13px;">Setter.Value</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;">    <</span><span style="color: #a31515; font-size: 13px;">ControlTemplate</span><span style="color: #ff0000; font-size: 13px;"> TargetType</span><span style="color: #0000ff; font-size: 13px;">="xyzCtlLibrary:Widgets"><br /> </span><span style="color: #0000ff; font-size: 13px;">    <!--  Control Template Xaml--><br /> </span><span style="color: #0000ff; font-size: 13px;">    </</span><span style="color: #a31515; font-size: 13px;">ControlTemplate</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;">   </</span><span style="color: #a31515; font-size: 13px;">Setter.Value</span><span style="color: #0000ff; font-size: 13px;">><br />   </span><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">Setter</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;"> </</span><span style="color: #a31515; font-size: 13px;">Style</span><span style="color: #0000ff; font-size: 13px;">><br /> </span><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">ResourceDictionary</span><span style="color: #0000ff; font-size: 13px;">></span> </p> ( Anand) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Tue, 04 Aug 2009 00:17:21 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment1516 Exceptionaly excelent explanation of this topic !!! five stars in my opinion .... thank you :-) ( Exorsus) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Fri, 24 Apr 2009 14:49:22 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment1382 I have been unable to get this to work... Has anyone else run into problems? I have tried added the .dll as a resource to blend 2. When I use the control it throws an error. <br /> <br /> While trying to build the LinkLabel in VS2008 I get errors about WrapPanel?!?! Saying it is a namespace but is used like a type... <br /> <br /> I am new to silverlight and my coding skills are rusty so need a little extra hand holding here. ( rollo) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Tue, 31 Mar 2009 17:45:54 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment1255 Very helpful. Cheers.<br /> ( voiddog) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Mon, 23 Mar 2009 12:38:52 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment1103 <p>great article, thanks.</p> ( freeze) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Tue, 24 Feb 2009 17:44:34 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment881 <p>I can't say thanks enough for you Man! Thank you so much! I've stuck into the generic.xaml problem for a couple of everlasting days. I've redone everything from the scratch several times, but I've not realized till now (thanks for your article) that a "/themes/generic.xaml" structure is a requirement, and I always created my own conventions. What a silly I am. So thank you Emil for this clear, unambiguous and great article!</p> ( Uchimata) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Tue, 06 Jan 2009 10:58:54 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment619 <p>This article is fantastic. I was searching internet for days and looking for such an article. I even read Silverlight Tutorials in Microsoft website but they were too complicated and I didn't understand what they were saying. This article is written to the point and thank you so much for making it easy.</p> ( Chandima) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Wed, 05 Nov 2008 13:00:34 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment582 <p>Hi, I'm not sure what exactly you are trying to do.</p> <p><font color="#606060">1:</font> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> DependencyProperty TextProperty = DependencyProperty.Register(</p> <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060"> 2:</span> <span style="color: #006080">"Text"</span>,</pre> <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060"> 3:</span> <span style="color: #0000ff">typeof</span>( <span style="color: #0000ff">string</span> ),</pre> <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060"> 4:</span> <span style="color: #0000ff">typeof</span>( LinkLabel ),</pre> <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060"> 5:</span> <span style="color: #0000ff">new</span> PropertyMetadata( <span style="color: #0000ff">new</span> PropertyChangedCallback( OnTextChanged ) ) );</pre> <p>This is the way you register the DependencyProperty Text. It is correct and should not give you any kind of error during compilation.</p> ( emil) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Thu, 23 Oct 2008 03:32:28 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment576 <p>Hi, I'm getting a compile error on this line in the customer class:</p> <p><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> DependencyProperty TextProperty = DependencyProperty.Register(</p> <p>It says it is looking for a delegate.  Am I missing something or did I forget a step?  Thanks!</p> ( Having difficulty) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Wed, 22 Oct 2008 08:45:05 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment550 <p>Thanks for getting me started on SilverLight Custom Controls, it is a great article. I would like to thank Sad for confirming that devotees of Open Source are short sighted bigots.   </p> ( Fleckney) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Thu, 16 Oct 2008 01:32:38 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment275 <p>Geoffrey, </p> <p>Here are two more articles on creating custom controls:</p> <p><a href="http://www.silverlightshow.net/items/Custom-ContentControl-using-VisualStateManager.aspx">Custom ContentControl using VisualStateManager</a></p> <p>and</p> <p><a href="http://www.silverlightshow.net/items/How-to-inherit-from-ItemsControl-and-create-a-UniformGrid-with-containers.aspx">How to inherit from ItemsControl and create a UniformGrid with containers</a></p> ( emil) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Sun, 03 Aug 2008 22:52:20 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment272 <p>Yes very helpful. Any tips of taking this further?</p> ( Geoffrey Swenson) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Thu, 31 Jul 2008 16:08:31 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment256 <p>Unbelievably simple great "hello world" article.  Saved me tons of head scratching and therapy expense.</p> ( Richard Fencel) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Tue, 22 Jul 2008 17:15:08 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment214 <p>Thanks for the article! it's great.</p> <p>To Sad,</p> <p>This is not a good place for you to whine around. You should stick with your open standards may be.</p> ( Chris) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Fri, 04 Jul 2008 00:36:55 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment179 <p>To the author of the article: Thank you for a very informative tutorial.</p> <p>To the writer of the comment above: This was a technical article. Your comments have completely missed the point.  Nobody really cares about your lamenting.</p> ( Dejan) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Tue, 17 Jun 2008 00:42:10 +0300 RE: Creating a Silverlight Custom Control - The Basics http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx#comment177 <p>So if Silverlight is so cool in regards to creating LinkButtons (or LinkLabels), why not re-create this website with XAML instead of HTML? Ohh yeah, that's right; because it's ActiveX technology, instable doesn't get indexed by search engines which means it won't attract visitors from Google and generally not suitable for creating web-stuff...</p> <p>Market Silverlight as what it is and not what you're hoping it to be. Flash 2.0, JavaApplets 2.0 or even ActiveX 2.0. Then again, if you did then developers won't really bother to invest in it then I guess ;)</p> <p>Though I think this will be its curse anyway...</p> <p>You guys are big, but you're NOT big enough (at least not now) to combat the _ENTIRE_ web alone. How about building on Open Standards instead of continuing de-commoditizing Open Web standards and protocols (ref; Halloween)</p> <p>Maybe you'd get to at least fade out with PRIDE and people remembering you for what you did which actually was GOOD which was putting a computer in front of (almost) every human on the planet instead of what you're risking now which is that history will remember you as the guys who consistantly refused to fade away without trying to drag the entire world with them...</p> <p>Sad... :(</p> <p>Every single waken analyst with more than two intact braincells knows that Silverlight will _NOT_ prevail, you guys really should get some better advice... :(</p> <p>You guys have so much power and so much money, imagine how much GOOD that could do in regards to innovation. Instead you're consistantly choosing to harm innovation... :(</p> <p>And you won't even succeed...</p> <p>At the end of the day it'll all end up as money being poured into a bottomless hole...</p> <p>Sad! :(</p> ( Sad) http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx Mon, 16 Jun 2008 18:12:40 +0300