Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Problem with Background Property in UserControl
7 replies. Latest Post by odahan on September 4, 2009.
(0)
dbaechtel
Member
59 points
207 Posts
07-31-2008 7:44 PM |
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
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
Participant
1129 points
260 Posts
08-01-2008 1:41 AM |
have you inhereted this class from any control?....else overriding willnot work...
08-01-2008 8:17 AM |
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 L...
All-Star
25052 points
2,747 Posts
08-04-2008 2:46 AM |
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.
moemeka
147 points
69 Posts
08-04-2008 3:04 AM |
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.
Somnath ...
118 points
107 Posts
09-17-2008 4:26 AM |
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…!! Please help..
Hurrikam
2 points
1 Posts
10-31-2008 3:44 PM |
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
1625 points
291 Posts
09-04-2009 3:18 PM |
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 !