Skip to main content

Microsoft Silverlight

Unanswered Question custom control in Silverlight 1.1RSS Feed

(0)

zamzam
zamzam

Member

Member

12 points

12 Posts

custom control in Silverlight 1.1

If you are creating a custom control in Silverlight 1.1 and shadowing properties (using the new keyword) that already exist on the Control class. If you set these properties in xaml nothing happens. But it works if set the properties in code.

Cass
Cass

Contributor

Contributor

3157 points

654 Posts

Re: custom control in Silverlight 1.1

Can u give us an example?  also the property code?

MarkTap
MarkTap

Participant

Participant

1442 points

263 Posts

Re: custom control in Silverlight 1.1

Do the parent versions of the properties get set from XAML? I.e. is it really "nothing" happening or is it just using the parent property setters?

 

zamzam
zamzam

Member

Member

12 points

12 Posts

Re: custom control in Silverlight 1.1

Here's what i am doing.

I created a class library using the Silverlight Class Library template in VS 2008 beta 2  and then I added a custom control (MyControl) using the template for Usercontrol.

Contents of MyControl.xaml.....

<Canvas xmlns="http://schemas.microsoft.com/client/2007"  xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml 

Width="200" Height="200" Background="Brown"></Canvas>

Contents of MyControl.xaml.cs......

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace CustomControls

{

public class MyControl : Control

{

FrameworkElement root;
public MyControl()

{

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("CustomControls.MyControl.xaml");

root = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());

}

public new double Height

{

get{return root.Height;}set{root.Height = value;}

}

public new double Width

{

get{return root.Width;}

set{root.Width = value;}

}

}

}

Then I created a test application using Silverlight Project template in VS 2008 beta 2.

Contents of Page.xaml.....

<Canvas x:Name="parentCanvas"

xmlns="http://schemas.microsoft.com/client/2007"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:cc="clr-namespace:CustomControls;assembly=ClientBin/CustomControls.dll"

Loaded="Page_Loaded"

x:Class="TestControl.Page;assembly=ClientBin/TestControl.dll"

Width="640"

Height="480"

Background="LightBlue"

>

<
cc:MyControl x:Name="control1" Canvas.Left="50" Canvas.Top="100" Height="100" Width="100"/>

<cc:MyControl x:Name="control2" Canvas.Left="300" Canvas.Top="100"/>

</Canvas>

 Contents of Page.xaml.cs.....

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace TestControl

{

public partial class Page : Canvas

{

public void Page_Loaded(object o, EventArgs e)

{

// Required to initialize variables

InitializeComponent();

control2.Height = 100;

control2.Width = 100;

}

}

}

Height and Width for 'control1' does not get affected but it works for 'control2'

MarkTap
MarkTap

Participant

Participant

1442 points

263 Posts

Re: custom control in Silverlight 1.1

What are base.Width and base.Height for control1? I don't have time to try out your code right now, but it's probably using the parent Width and Height properties. I think you just need to set them in code for now - hopefully a subsequent release of Silverlight will have better integration of custom controls.
 

tanmoy.r
tanmoy.r

Contributor

Contributor

3594 points

710 Posts

Re: Re: custom control in Silverlight 1.1

xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml 

is it a typo? without quote?

Please Mark as Answer if this helps you.
Thanks n Regards
~Tanmoy
Blog: http://anothersilverlight.blogspot.com/

balla
balla

Member

Member

674 points

200 Posts

Re: custom control in Silverlight 1.1

I tried this and it really doesn't work! Putting a break-point into the set-properties that are created using the "new" keyword don't get called when initalizing through XAML. Whereas those without "new" are called properly when set using XAML. Using code-behind language to set the properties works fine for both. BUG (or feature)? PS: That's not a typo. This forum sometimes messes up links inserted into the Message.

Psychlist1972
Psychlis...

Contributor

Contributor

6045 points

973 Posts

MicrosoftModerator

Re: custom control in Silverlight 1.1

It's hard to say something is a bug when the whole thing is Alpha, but that's where I'd lean.

Since the control model is going to be overhauled, I would hope/expect this to change to however WPF would handle this situation.

Pete

Silverlight.net Moderator
MVP: Silverlight, Silverlight Insider
Author: Silverlight in Action, 2nd Edition
POKE 53280,0 - My Blog

Opinions are my own. Oh, and I don't work for Microsoft.

i-mameir
i-mameir

Member

Member

38 points

13 Posts

Re: custom control in Silverlight 1.1

Hi!

 it is not necessary to set the Width and Height Property! You get those values with "this.Width" and "this.Height"

 if you try to overwrite these properties, it won't work!

Just don't code the properties "Width" and "Heigth" in your control, you will get the values anyway with "this.Height".

 

Psychlist1972
Psychlis...

Contributor

Contributor

6045 points

973 Posts

MicrosoftModerator

Re: custom control in Silverlight 1.1

i-mameir:

Hi!

 it is not necessary to set the Width and Height Property! You get those values with "this.Width" and "this.Height"

 if you try to overwrite these properties, it won't work!

Just don't code the properties "Width" and "Heigth" in your control, you will get the values anyway with "this.Height".

I know what he's trying to do, though, and it is a legit problem. When you set the width and height of the control, it doesn't change the root canvas width/height. You have to do that yourself. He was hoping (and it makes perfect sense) to encapsulate all that in an override of the Height/Width properties.

Pete

Silverlight.net Moderator
MVP: Silverlight, Silverlight Insider
Author: Silverlight in Action, 2nd Edition
POKE 53280,0 - My Blog

Opinions are my own. Oh, and I don't work for Microsoft.

i-mameir
i-mameir

Member

Member

38 points

13 Posts

Re: custom control in Silverlight 1.1

yep i know ... but it seems not to work in alpha release :(

 i hope they fix it soon though ...

philcurnow
philcurnow

Member

Member

54 points

12 Posts

Re: custom control in Silverlight 1.1

I came across the same problem when writing some custom controls. I had a search around on the net and found an entry on Jeff Prosise's blog about this. Aparently this is a bug in the Alpha release. I have put a link to his blog entry below.

Jeff Prosise's Blog Entry on this problem

 

Phil Curnow
http://www.curnow.biz
http://blog.curnow.biz

Daliyot
Daliyot

Member

Member

2 points

1 Posts

Re: custom control in Silverlight 1.1

Has anyone else got it working directly from XAML without using the "new" keyword (like balla did)?

For me it didn't work with or without it.

 

VladF
VladF

Member

Member

216 points

87 Posts

Re: custom control in Silverlight 1.1

I use "shadow" string properties for my own properties which have custom type (enumeration for example):

public HorizontalAlignment HorizontalAlignment

{

get { return _HorizontalAlignment; }

set

{

if (_HorizontalAlignment != value)

{

_HorizontalAlignment =
value;

DoHorizontalAlignmentChanged();

}

}

}

public string HorizontalAlignment_

{

set { HorizontalAlignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), value, true); }

}

So I can set HorizontalAlignment_ in XAML and have correct value loaded into the HorizontalAlignment from XAML. This technique can also be used for Width/Height, but it will be useless if you use some tool for XAML editing.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities