<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.silverlight.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx</link><description>In my new video I demonstrate how you can create controls dynamically at run time. The technique is very straight forward, everything you can create declaratively in XAML you can also create dynamically in C#. xaml &amp;lt;Button x:Name=&amp;quot;Button1&amp;quot;</description><dc:language>en</dc:language><generator>CommunityServer 2007 (Build: 20416.853)</generator><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#233776</link><pubDate>Tue, 16 Jun 2009 06:08:54 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:233776</guid><dc:creator>rajkra</dc:creator><description>&lt;p&gt;How can we find the control which was created dynamically?&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=233776" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code | My Geek Solutions | My Geek ??? | My &amp;#8230; | My Geek Solutions</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#54411</link><pubDate>Tue, 20 May 2008 23:19:30 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:54411</guid><dc:creator>re: Creating Controls in Code | My Geek Solutions | My Geek ??? | My … | My Geek Solutions</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;re: Creating Controls in Code | My Geek Solutions | My Geek ??? | My &amp;amp;#8230; | My Geek Solutions&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=54411" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code | My Geek Solutions | My Geek &amp;#8230; | My Geek Solutions</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#54390</link><pubDate>Tue, 20 May 2008 20:46:10 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:54390</guid><dc:creator>re: Creating Controls in Code | My Geek Solutions | My Geek … | My Geek Solutions</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;re: Creating Controls in Code | My Geek Solutions | My Geek &amp;amp;#8230; | My Geek Solutions&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=54390" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#54303</link><pubDate>Tue, 20 May 2008 12:09:12 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:54303</guid><dc:creator>SteveFrench</dc:creator><description>&lt;p&gt;How do you set the x:Name property programatically with dynamic controls? &amp;nbsp;Silverlight doesn't seem to want to let you for some reason...&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=54303" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#53341</link><pubDate>Thu, 15 May 2008 02:27:13 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:53341</guid><dc:creator>jesseliberty</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;I want to have an event to last while the left mouse is down (like having an object &amp;quot;rotate&amp;quot; while the left mouse is down and then stop rotating when the left mouse is up). &amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;I think I would start the rotate on the MouseDown event and stop the rotate on the MouseUp event.&lt;/p&gt;
&lt;p&gt;// pseudocode&lt;/p&gt;
&lt;p&gt;myObject_OnMouseUp(object o, MouseEventArgs e)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;rotating=false;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;-j&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=53341" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#53265</link><pubDate>Wed, 14 May 2008 16:37:58 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:53265</guid><dc:creator>raspa2k</dc:creator><description>&lt;p&gt;Jessy,&lt;/p&gt;
&lt;p&gt;Thank you for your post, very helpful.&lt;/p&gt;
&lt;p&gt;I have a question that is not directly related with this article but probably interesting for the community since is related with handling events&lt;/p&gt;
&lt;p&gt;I want to have an event to last while the left mouse is down (like having an object &amp;quot;rotate&amp;quot; while the left mouse is down and then stop rotating when the left mouse is up). &lt;/p&gt;
&lt;p&gt;How can I accomplish the &amp;quot;while mouse is down&amp;quot; event functionality ? I notice that the event triggers properly but the code runs only once. When I coded with SL1.1 I used to handle it within Javascript with the setTimeout method.&lt;/p&gt;
&lt;p&gt;Thank you for your time and all your help&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=53265" width="1" height="1"&gt;</description></item><item><title>Weekly Links: ASP.NET MVC, Silverlight 2, TDD, WPF, jQuery&amp;#8230; | Code-Inside Blog International</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#53203</link><pubDate>Wed, 14 May 2008 11:51:28 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:53203</guid><dc:creator>Weekly Links: ASP.NET MVC, Silverlight 2, TDD, WPF, jQuery… | Code-Inside Blog International</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Weekly Links: ASP.NET MVC, Silverlight 2, TDD, WPF, jQuery&amp;amp;#8230; | Code-Inside Blog International&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=53203" width="1" height="1"&gt;</description></item><item><title>W&amp;ouml;chentliche Rundablage: ASP.NET MVC, Silverlight 2, TDD, WPF, jQuery&amp;#8230; | Code-Inside Blog</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#53202</link><pubDate>Wed, 14 May 2008 11:49:44 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:53202</guid><dc:creator>Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, TDD, WPF, jQuery… | Code-Inside Blog</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;W&amp;amp;ouml;chentliche Rundablage: ASP.NET MVC, Silverlight 2, TDD, WPF, jQuery&amp;amp;#8230; | Code-Inside Blog&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=53202" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#51470</link><pubDate>Tue, 06 May 2008 13:30:15 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51470</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;(1) All of this is in Silverlight (not ASP.NET, etc.) &amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;Yes!&lt;/p&gt;
&lt;p&gt;------&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;Your design says that I have a standard user control, which I have, say a grid (call it LayoutRoot,&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;Yes!&lt;/p&gt;
&lt;p&gt;-----&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;At the top of the grid I have two buttons: Create and Close. Create dynamically instantiates a user control of type Container and Close makes it go away. As far as I read your design, both buttons are outside the container.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;No! The create button is on the main page that creates the User Control (This user control is like a child form) which has it's OWN &amp;quot;Close&amp;quot; button. The &amp;quot;Close&amp;quot; button&amp;quot; is on the the User Control. And for now, by clicking, I set the visibility of the User Control to Collapsed.&lt;/p&gt;
&lt;p&gt;-----&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;(1) what happens if I click Create twice?&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;When I show the User Control, It also has a Rectangle underneath that stretches the entire screen, which prevents the user to access the main form to click again. It's like a modal window!&lt;/p&gt;
&lt;p&gt;-----&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;(2) Why not have close be within the container so we know which container is being closed intuitively?&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;As mentioned, the Close IS on the container!&lt;/p&gt;
&lt;p&gt;-----&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;(3) why are you collapsing the container on close and then setting it to null? Either you want to make it invisible or you want to destroy it.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;For now, I'm setting it to Collapsed to make it invisible, because I had created the UC at design time. However, once I change my design to create it dynamically, I'd then raise an even in the Close button and the parent would remove it from children and set it to null and I don't need to set it to collapse anymore.&lt;/p&gt;
&lt;p&gt;-----&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;Each user control has, in addition to whatever else it has, a close button with a click event to which the stack panel subscribes. On click, the stack panel is told the ID of the panel that is closing and the stack panel removes it from its children collection - this both makes the user control invisible and moves everything below it in the stack up into position (if you use a grid, you have either a hole in your grid or you have to move them yourself.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;I have already planned the formation of my UC. That's not an issue. &lt;/p&gt;
&lt;p&gt;-----&lt;/p&gt;
&lt;p&gt;My only point and question is, when an action (i.e. a button on UC is pressed), I need that action to be broadcast, so in the parent an event handler would respond (as you said remove from children and set the reference to null). That's where I need the help!&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51470" width="1" height="1"&gt;</description></item><item><title>Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#51462</link><pubDate>Tue, 06 May 2008 13:06:04 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51462</guid><dc:creator>Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51462" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#51455</link><pubDate>Tue, 06 May 2008 12:22:30 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51455</guid><dc:creator>jesseliberty</dc:creator><description>&lt;p&gt;Ben,&lt;/p&gt;
&lt;p&gt;I have a number of questions about your design. &amp;nbsp;First, Let me be sure i have this clear... (1) All of this is in Silverlight (not ASP.NET, etc.) &amp;nbsp;(2) Your design says that I have a standard user control, which I have, say a grid (call it LayoutRoot,&lt;/p&gt;
&lt;p&gt;Now, the heart of the design:&lt;/p&gt;
&lt;p&gt;At the top of the grid I have two buttons: Create and Close. Create dynamically instantiates a user control of type Container and Close makes it go away. As far as I read your design, both buttons are outside the container.&lt;/p&gt;
&lt;p&gt;So, a few questions: (1) what happens if I click Create twice? Do I get two containers? &amp;nbsp;Or does Create become disabled when one container is created?&lt;/p&gt;
&lt;p&gt;(2) Why not have close be within the container so we know which container is being closed intuitively?&lt;/p&gt;
&lt;p&gt;(3) why are you collapsing the container on close and then setting it to null? Either you want to make it invisible or you want to destroy it. &lt;/p&gt;
&lt;p&gt;So, what I'd suggest is that you use a stack panel as your main layout control to hold your created containers (or a grid, depending on how much work you want to do).&lt;/p&gt;
&lt;p&gt;At the top is a button Create. Clicking that button causes a new container (user control) to be added to the stackpanel's children collection (and thus be placed at the bottom of the panel). &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Each user control has, in addition to whatever else it has, a close button with a click event to which the stack panel subscribes. On click, the stack panel is told the ID of the panel that is closing and the stack panel removes it from its children collection - this both makes the user control invisible and moves everything below it in the stack up into position (if you use a grid, you have either a hole in your grid or you have to move them yourself.&lt;/p&gt;
&lt;p&gt;Yes, this would make a good video. I'll add it to the list.&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51455" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#51313</link><pubDate>Mon, 05 May 2008 22:54:05 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51313</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;Ben, by all means, ask your question and I'll try to answer if I can.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;Thank you Sir;&lt;/p&gt;
&lt;p&gt;Here is the situation.I'm creating an application that has many User Controls. Some of these UC are more like a full page (container to other controls or UC) and some are independent UC. So for the sake of this discussion, let's call the Container UC as Container.&lt;/p&gt;
&lt;p&gt;One way to use these container, is using Xaml and creating these containers at design time and setting their Visibility to Collapse and then at run time, turning the visibility back on again. I don't care for this method so much for Container situation. Number one, all these containers are always created when the application is loaded (and whether the user ever uses them or not) and secondly it clusters my main page by having all these UC but collapsed.&lt;/p&gt;
&lt;p&gt;A better way is to dynamically create them as the user (let's say clicks a button), the main form instantiates that container, and when he/she close the container, it's reference is set to null and is gone.&lt;/p&gt;
&lt;p&gt;And here is where I'm having a bit of problem (my lack of knowledge of creating delegate and even handler). &lt;/p&gt;
&lt;p&gt;Suppose, there is a button on the main form and when the user clicks on the button, a new container gets instantiated and the new container appears on the top of the main form. However, there is also a &amp;quot;Close&amp;quot; button on that container that when the user clicks on, the container closes. And here is my question. When the user clicks on the the &amp;quot;Close&amp;quot; button, I'll set the visibility of that Container to Collapsed, however I need to raise some kind of event in that Close button that the main form (who originally created the Container), will respond to this event that the container has been closed, therefore I need to remove it from the Children.Remove(Container) and then set the container to null.&lt;/p&gt;
&lt;p&gt;I don't know how to raise that event in the &amp;quot;Close&amp;quot; button and how to respond to it in the main form.&lt;/p&gt;
&lt;p&gt;A sample or explanation or snippet or pointer to some info is much appreciated!&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;..Ben&lt;/p&gt;
&lt;p&gt;p,s. I tried to learn it from your C# book (4th edition) but didn't get that far!&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51313" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#51298</link><pubDate>Mon, 05 May 2008 21:31:43 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51298</guid><dc:creator>jesseliberty</dc:creator><description>&lt;p&gt;Ben, by all means, ask your question and I'll try to answer if I can.&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51298" width="1" height="1"&gt;</description></item><item><title>re: Creating Controls in Code</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/05/creating-controls-in-code.aspx#51285</link><pubDate>Mon, 05 May 2008 20:57:53 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51285</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;Jesse, I really liked thus video, especially towards the end when you got into those tricky challenges. That were good things to learn from.&lt;/p&gt;
&lt;p&gt;I do have one technical question that is not 100% directly related to dynamic controls but related to dynamic User Controls. And I was wondering if I should take the time and write the question and if you would either give me a detail answer or perhaps write a blog about it, since many people will need the answer.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;..Ben&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51285" width="1" height="1"&gt;</description></item></channel></rss>