Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, so based on @Harlow Burgess answer, a managed to make a working example in Silverlight.</p> <p>Basically the difference is, that in SL, <code>DependencyObject</code> class has no <code>OnPropertyChanged</code> method, so in <code>DPGroup</code> class we cannot override it, but we can attach this method in another way, by: </p> <pre><code>new PropertyMetadata(1, OnPropertyChanged). </code></pre> <p>So the <code>DPGroup</code> class will look like this:</p> <pre><code>public class DPGroup : DependencyObject, INotifyPropertyChanged { 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); } } // static method invoked when MyProperty1 has changed value static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DPGroup g = d as DPGroup; if (g != null) { g.MyProperty1 = (int)e.NewValue; // invoking event handler, to notify parent class about changed value of DP if (g.PropertyChanged != null) g.PropertyChanged(g, null); } } // event handler, for use in parent class public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>And the parent class, containing dependency property of type <code>DPGroup</code>:</p> <pre><code>public partial class DPTest : UserControl { public static readonly DependencyProperty GroupProperty = DependencyProperty.Register( "Group", typeof(DPGroup), typeof(DPTest), new PropertyMetadata( new DPGroup(), new PropertyChangedCallback(OnGroupPropertyChanged) ) ); public DPGroup Group { get { return (DPGroup)GetValue(GroupProperty); } set { SetValue(GroupProperty, value); } } // static method invoked when Group property has changed value // here we need to attach event handler defined if DPGroup, so it will fire from inside Group property, // when Group.MyProperty1 will change value static void OnGroupPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DPTest control = (DPTest)d; DPGroup oldGroup = e.OldValue as DPGroup; // removing event handler from prevoius instance of DBGroup if (oldGroup != null) oldGroup.PropertyChanged -= new PropertyChangedEventHandler(control.group_PropertyChanged); DPGroup newGroup = e.NewValue as DPGroup; // adding event handler to new instance of DBGroup if (newGroup != null) newGroup.PropertyChanged += new PropertyChangedEventHandler(control.group_PropertyChanged); DPTest g = d as DPTest; if (g != null) control.UpdateTextBox(); } private void group_PropertyChanged(object sender, PropertyChangedEventArgs e) { UpdateTextBox(); } // here you can do anything with changed value Group.MyProperty1 private void UpdateTextBox() { this.textBox1.Text = this.Group.MyProperty1.ToString(); } public DPTest() { InitializeComponent(); } } </code></pre> <p>Now, the XAML part for <code>DPTest</code>:</p> <pre><code>&lt;UserControl x:Class="Silverlight_Workbench_2.DPTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Silverlight_Workbench_2" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" &gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="76,61,0,0" x:Name="textBox1" VerticalAlignment="Top" Width="120" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Finally, we can embed our <code>DPTest</code> in some content of any control, for example in a <code>Grid</code> of another user control:</p> <pre><code>&lt;UserControl x:Class="Silverlight_Workbench_2.DPTestMain" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Silverlight_Workbench_2" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;local:DPTest&gt; &lt;local:DPTest.Group&gt; &lt;!--here we can change value, and it will be reflected in design window as a text in textBox1--&gt; &lt;local:DPGroup MyProperty1="8"/&gt; &lt;/local:DPTest.Group&gt; &lt;/local:DPTest&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>That is all, thanks again to the Harlow Burgess for the help!</p>
    singulars
    1. This table or related slice is empty.
    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.
    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