Note that there are some explanatory texts on larger screens.

plurals
  1. POdependency properties gathered in separate class
    primarykey
    data
    text
    <p>My question concerns Silverlight (but I guess WPF as well).</p> <p>Basically I know, how to create dependency property in a user control and how to make it work. But what i was trying to do, and didn't succeded is: to create dependency property (or more than one) in a class, and this class will become a dependency property for my user control.</p> <p>With other words: </p> <pre><code>// my UserControl public class DPTest : UserControl { // dependency property, which type is a class, and this class will be holding other dependency properties public static readonly DependencyProperty GroupProperty = DependencyProperty.Register("Group", typeof(DPGroup), typeof(DPTest), new PropertyMetadata(new DPGroup(), OnPropertyChanged)); public DPGroup Group { get { return (DPGroup)GetValue(GroupProperty); } set { SetValue(GroupProperty, value); } } // this occurs only when property Group will change, but not when a member of property Group will change static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DPTest g = d as DPTest; // etc. } } // a class, where I want to hold my dependency properties public class DPGroup : DependencyObject { public static readonly DependencyProperty MyProperty1Property = DependencyProperty.RegisterAttached("MyProperty1", typeof(int), typeof(DPGroup), new PropertyMetadata(1, OnPropertyChanged)); public int MyProperty1 { get { return (int)GetValue(MyProperty1Property); } set { SetValue(MyProperty1Property, value); } } // I would like to notify "the parent" (which means user control "DPTest" ), that member MyProperty1 has changed static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DPTest g = d as DPTest; if (g != null) g.textBox1.Text = g.Group.MyProperty1.ToString(); } } </code></pre> <p>What I want to achieve is <b>to notify (in design time in XAML) a user control <code>DPTest</code>, that member of <code>Group</code> property (<code>Group.MyProperty1</code>) changed it's value</b>. I managed to make it happen in a run-time, for example by using event handler defined in <code>DPGroup</code> class, but this doesn't work in design-time in xaml.</p> <pre><code>&lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;local:DPTest&gt; &lt;local:DPTest.Group&gt; &lt;local:DPGroup MyProperty1="2"/&gt; &lt;/local:DPTest.Group&gt; &lt;/local:DPTest&gt; &lt;/Grid&gt; </code></pre> <p>It works, but only first time, during creating tag:</p> <pre><code> &lt;local:DPGroup MyProperty1="2"/&gt; </code></pre> <p>and after this, changing value of <code>MyProperty1</code>, does not fire <code>DPTest.OnPropertyChange</code>. Probably fires <code>DBGroup.OnPropertyChanged</code>, but this of course does not notify user control <code>DPTest</code> about it. <b>So how to make <code>DPTest</code> know, that the <code>Group.MyProperty1</code> has changed?</b></p> <p>I don't want to make any bindings from <code>MyProperty1</code> to respective property created inside user control <code>DPTest</code> (not to duplicate properties), the point is to have a group of properties in separate class, so i can use this group more than once, like:</p> <pre><code>// my UserControl public class DPTest : UserControl { public DPGroup Group1 { ... } public DPGroup Group2 { ... } } </code></pre> <p>I see some analogy to <code>UIElement.RenderTransform</code> (let's say it is my <code>Group</code> property) which holds for example <code>ScaleTransform</code></p> <pre><code>&lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;Grid.RenderTransform&gt; &lt;ScaleTransform ScaleX="0.4"/&gt; &lt;/Grid.RenderTransform&gt; &lt;/Grid&gt; </code></pre> <p><code>ScaleX</code> is an analogy to <code>MyProperty1</code>. <b>The difference is, that changing value of <code>ScaleX</code> (in XAML) will reflect immediate changes in design-time, and exactly this I am trying to achieve.</b></p> <p>I was trying to find a solution in entire google/stack overflow and others, but none found. Everywhere are just examples of creating dependency properties inside a user control.</p> <p>Thank you for your time. Any help much appreciated.</p> <p>edit: based on Harlow Burgess answer, a managed to make a working example in Silverlight. I put the whole solution below as an separate answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload