Skip to main content

Microsoft Silverlight

Unanswered Question Problem with Background Property in UserControlRSS Feed

(0)

dbaechtel
dbaechtel

Member

Member

59 points

207 Posts

Problem with Background Property in UserControl

I am working on a Silverlight 2.0 B2 application using VSTS 2008.

I have a UserControl MyControl that consists of a TextBox, ListBox and some other elements.

I want to use the Background property the the XAML delaration of MyControl to set the Background property of some of the components within.

In the code for MyControl I have written:

Public Overloads Property Background() As Brush
Get
  Return _background
End Get
Set(ByVal value As Brush)
_background = value
End Set
End Property

In the XAML I have:
<my:MyControl x:Name="X" LimitTo="Items"  Margin="5" Background="LightYellow" />

In Debug mode, breakpoints say that my Background Set method is not being called and background  is never set.

LimitTo is a similar custom property that is being called correctly.

I am assuming  that Background is not being called because it "Shadows" the Background Property of the UserControl type.
But I have declared my method as Overloads.

Why is my Background property methods not being called?
What do I have to do to override and capture this property setting?

 

THANKS for any help.

 

Shaji-mji
Shaji-mji

Participant

Participant

1129 points

260 Posts

Re: Problem with Background Property in UserControl

 have you inhereted this class from any control?....else overriding willnot work...

dbaechtel
dbaechtel

Member

Member

59 points

207 Posts

Re: Problem with Background Property in UserControl

Shaji-mji:

 have you inhereted this class from any control?....else overriding willnot work...

It Inherits UserControl which is a System.Windows.Controls.Control and  includes a Background property.
Why wouldn't that work?

 

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Problem with Background Property in UserControl

Hello, first you should use the Shadows keyword. Then currently XAML doesn't support property hide. Even if you use the Shadows keyword, if you try to set the property in XAML, it won't have any effect. But you can set the property in code. So I think you should create a new name for your property.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

moemeka
moemeka

Member

Member

147 points

69 Posts

Re: Problem with Background Property in UserControl

create an event handler for your usercontrol's Load event and set the background property of whatever internal element you want to this.Background.  Once you rebuild, changing the background property in xaml will even be reflected in the designer.  

Please mark as answered if this post resolves your issue.

http://moemeka.blogspot.com

Somnath Panja
Somnath ...

Member

Member

118 points

107 Posts

Re: Re: Problem with Background Property in UserControl

I’m facing the same problem.

I’m unable to override the background property.  I feel there is problem with Silverlight for all properties defined in Control.

I want to override Background property to fire an event on background change.

Even I have tried to do binding with an internal dependency property with background that on change of background at least my internal property should be able to fire the event. That is also not working.

What should I do…!!Sad Please help..

Hurrikam
Hurrikam

Member

Member

2 points

1 Posts

Re: Problem with Background Property in UserControl

Hi,

I was experiencing the same problem in the same case.

But looking at a different post http://silverlight.net/forums/p/20783/72428.aspx#72428 (which shows how uncomplete is still SL compared to WPF) I found the missing part of the solution:

Just create your internal 'rootPanel' (like a Grid) and set it to the Content property of your extended UserControl. Then data binds the two Background properties of you UserControl and its child:

public UserControlEx() : base()
{
this.Content = this.rootPanel;
Binding binding = new Binding("Background");
binding.Source = rootPanel;
binding.Mode =
BindingMode.TwoWay;
this.SetBinding(Control.BackgroundProperty, binding);
}

This is a preferred way to hiding members from the base class. Hope this helps.

-Hurrikam

odahan
odahan

Participant

Participant

1625 points

291 Posts

Re: Re: Problem with Background Property in UserControl

I think this post should give you at least 10000 points :-)

2 days I'm looking for a solution and can't find any valid answer.

Of course the real solution will be given when SL will support dependency property metadata override, but your trick is simple and it does the job.

Thanks again !

Olivier Dahan (MVP C#)
wwww.e-naxos.com
Blog: www.e-naxos.com/blog
English is not my native language so be indulgent and do not hesitate to ask for clarification! Thanks.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities