Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
What is the best way to inherit Dependency Properties values. For example I have this code and it doesn't work correctly:
C#
 // UserControl1 contains only TextBox 
public partial class UserControl1 : UserControl
 {
     public static readonly DependencyProperty TextProperty = TextBox.TextProperty.AddOwner(typeof(UserControl1), new FrameworkPropertyMetadata("any text";, FrameworkPropertyMetadataOptions.Inherits, (d,o) => Debugger.Break()));

     public string Text
     {
         get { return this.GetValue(UserControl1.TextProperty).ToString(); }
         set { this.SetValue(UserControl1.TextProperty, value);}
     }
     public UserControl1()
     {
         InitializeComponent();
     }
 }


I added UserControl1 to Window and set Text property in Window's XAML file. Value changed only in UserControl1 instance but didn't change TextBox.Text.
Posted
Updated 12-Dec-10 21:56pm
v4
Comments
Abdul Quader Mamun 13-Dec-10 3:50am    
Spelling check.

1 solution

you have to inherit DependencyObject to achieve this like


VB
public class MyTextBox : DependencyObject
{
//
// Dependency property for Text
//
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(MyTextBox));
//
// Change-aware property
//
public string Text
    get { return base.GetValue(TextProperty) as string; }
set { base.SetValue(TextProperty, value); }
}
}



Hope this helps
:)
 
Share this answer
 
Comments
paulkr 17-Dec-10 17:35pm    
The code what You have pasted here is not an answer how to connect parent Dependency Property value with child Dependency Property value. This is a very basic Dependency Properties usage sample.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900