locked
Custom Control Width & Height properties RRS feed

  • Question

  • Hey guys.  I'm a new member, and also new to Silverlight & WPF.

    Fairly ridiculous question here, but here it goes.. Once I add a custom control to my canvas, I cannot programmatically set the width or height of the control.  Seems that the only time its being set is when its loaded from the xaml.  I can set both the Top and Left properties, but not Width and Height..  How exactly do you set the width and height of a control? Programmatically, not in XAML..

     I have alot of custom control experience in Win32, so I guess I'm a bit embarrassed to be even asking how to perform such a simple task.
     

    Thanks alot.


     

    Sunday, October 28, 2007 10:54 PM

Answers

  • Are you setting .Width and .Height and not seeing any changes? If so, you need to also change the width and height of your parent canvas inside the usercontrol (you need to give it an x:Name to do that easily). If you want scaling, you need to associate a scaletransform render transform with that canvas and set that instead.

    The usercontrol model in SL 1.1 is very nice, but it is a little rough around the edges right now.

    Pete

    Monday, October 29, 2007 9:44 AM

All replies

  • I'll assume you are doing this inside a Silverlight Project in VS2008 beta and that your custom control is on your main page (i.e. declared in the file Page.xaml). If these two conditions are true, then it should be straightforward. Your Page.xaml will contain something like this (notice where I assigned a name to the control with x:Name).

        <Canvas ... xmlns:custom="clr-namespace:MyControls;assembly=ClientBin/MyControls.dll">
          <custom:MyControl x:Name="myControl" />
        </Canvas>

    Because this is in your Page.xaml, most of the work is done for you. In the generated Page class in Page.xaml.cs, you will have a field named "myControl" of type "MyControl". You should be able to just say:

        myControl.Width = 50; 

     If my assumptions are not correct, please reply with some more detail about the structure of your code (e.g. if you are using the custom control inside *another* custom control instead of on your main page, that changes things).

    Sunday, October 28, 2007 11:41 PM
  •  Hello. Please have a look at the following link - it might help to resolve the problem. http://silverlight.net/forums/p/1234/2855.aspx

    Monday, October 29, 2007 6:30 AM
  • Are you setting .Width and .Height and not seeing any changes? If so, you need to also change the width and height of your parent canvas inside the usercontrol (you need to give it an x:Name to do that easily). If you want scaling, you need to associate a scaletransform render transform with that canvas and set that instead.

    The usercontrol model in SL 1.1 is very nice, but it is a little rough around the edges right now.

    Pete

    Monday, October 29, 2007 9:44 AM