4 Replies - 6246 Views - Last Post: 23 January 2013 - 06:16 AM

#1 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Procedurally adding control

Posted 20 January 2013 - 12:19 PM

Hello,

I have just starting exploring programming apps for the windows phone platform.

I have a little experience programming c# in .net framework. When is first jumped into a windows phone project, i obviously noticed the big difference, which is that UI elements are handled using XAML. I am not familiar with this at all, but I have figured it out ok and it seems straightforward.

But now what I want to try to do it have a button, where when I click it, at textbox is added to the mainpage. I am having trouble. In a windows forms program I could just ad this.Controls.add in the button_click method. It is not the same in a windows phone app.

I have thought that maybe I need to add to one of the control grid aka this.LayoutRoot.someaddmethod???

Anyways I am sure you get the picture of what I am trying to do. Can anyone give me a little help? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Procedurally adding control

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: Procedurally adding control

Posted 20 January 2013 - 01:48 PM

To learn XAML programming let's first take away the limitations of Windows Phone.

Learning XAML is more than just a quickie snippet we can hand you in a thread.

There are numerous books and tutorials about WPF (Windows Presentation Foundation), and that is what this is whether it is on a phone or on a desktop application.

My suggestion is to pick up a book on WPF programming and get a feel for this just like you did with WinForms: Start with 'Hello World' in a WPF flavor and move forward from there.

If you search for how to do these things using 'WPF' you generally get good answers.

FOr example a search for 'WPF add control runtime' gets good links such as
http://social.msdn.m...bd-319440b208e5
Was This Post Helpful? 0
  • +
  • -

#3 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: Procedurally adding control

Posted 21 January 2013 - 06:45 PM

Thanks I will look into it. I really had no clue Windows phone API is based on WPF. Just knowing that alone will help a lot.

One more question. Is adding some procedurally with code, the way I have gone about it, still the best way to achieve this, or is there a better way using xaml?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: Procedurally adding control

Posted 21 January 2013 - 06:54 PM

I think that really depends on the overall design and purpose of the application. There is no one right way to do something across all applications.

If you are swapping out lots of various controls it might be good.

But if you are just wanting to hide and show a few things depending on values it might be better to build the complete GUI in the designer, but setting the .Visibility property to Collapsed when you don't want it shown.

Collapsed is a new state of visibility over just the boolean used in WinForms.

Visibility.Visible = well, duh, its visible
Visibility.HIdden = Not shown, but the space is reserved and blank
Visibiilty.Collapsed = Not show, and the space is collapsed so other controls can move up or over to fill the space.

This way you can make some fairly cool and exotic looking controls using the designer.

Also realize that in WPF you can define styles similar to web page CSS. So if you define a style for your groupboxes to be blue lines, rounded corners, border thickness of 14 on the left edge and 5 on the right edge... You only define that in one place then all your group boxes follow that. Chanage the style and all your boxes change to match. That kind of hierarchy is easier to see and follow if you do it in the designer.
Was This Post Helpful? 1
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Procedurally adding control

Posted 23 January 2013 - 06:16 AM

Just to answer the question on how to add a control programmatically, most of the "parent" controls, like StackPanel, Grids, etc., normally have a Children property. That property has an Add method that allows you to add children to the control. That's normally how you add controls programmatically.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1