locked
Access Base custom class dependency property as derived custom class dependency property RRS feed

  • Question

  • Hi there, I need to inherit a custom control from another custom control, and I also need to access the dependency property of the base control.


    the code for base control ..........

     

      public class BaseControl : Control
    
        {
    
           public static DependencyProperty CtrlValue;
    
    
    
            static BaseControl()
    
            {
    
                DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseControl), new FrameworkPropertyMetadata(typeof(BaseControl)));
    
                CtrlValue = DependencyProperty.Register("ControlValue", typeof(string), typeof(BaseControl), new UIPropertyMetadata("0", new PropertyChangedCallback(CountChanged)));
    
            }
    
    
    
            static void CountChanged(DependencyObject ControlValue, DependencyPropertyChangedEventArgs args)
    
            {
    
    
    
                MessageBox.Show("Count is changed!" + args.NewValue.ToString());
    
            }
    
    
    
            public string ControlValue
    
            {
    
                get
    
                {
    
                    return (string)base.GetValue(CtrlValue);
    
                }
    
    
    
                set
    
                {
    
                   
    
                    base.SetValue(CtrlValue, value);
    
    
    
                }
    
            }
    
    
    
        }
    
    

    i added Base_control in references
    and the code for derived control .........

     public class DigitalControl : Base_Control.BaseControl
    
        {
    
            static DependencyProperty offValue;
    
            static DependencyProperty offColor;
    
           
    
            static DigitalControl()
    
            {
    
                Base_Control.BaseControl.CtrlValue.OverrideMetadata(typeof(DigitalControl), new PropertyMetadata(Base_Control.BaseControl.CtrlValue, new PropertyChangedCallback(OnPropertyChanged)));
    
    
                offValue = DependencyProperty.Register("StopStatusValue", typeof(double), typeof(DigitalControl), new PropertyMetadata(10.0, new PropertyChangedCallback(offvChanged)));
    
                 offColor = DependencyProperty.Register("StopStatusColor", typeof(string), typeof(DigitalControl), new PropertyMetadata(string.Empty, new PropertyChangedCallback(offcChanged)));
    
            }
    
    
    
            static void OnPropertyChanged(DependencyObject ControlValue, DependencyPropertyChangedEventArgs args)
    
            {
                MessageBox.Show("Count is changed!" + args.NewValue.ToString());
    
            } 
    
    
    
            static void offvChanged(DependencyObject ControlValue, DependencyPropertyChangedEventArgs args)
    
            {
    
               MessageBox.Show("Count is changed!" + args.NewValue.ToString());
    
            }
          
    
    
            static void offcChanged(DependencyObject ControlValue, DependencyPropertyChangedEventArgs args)
    
            {
    
                MessageBox.Show("Count is changed!" + args.NewValue.ToString());
    
            }
    
    
    
            public double StopStatusValue
    
            {
    
                get
    
                {
                    return (double)base.GetValue(offValue);
    
                }
    
                set
    
                {
    
                     base.SetValue(offValue, value);
    
                }
            }
    
    
            public string StopStatusColor
    
            {
    
                get
    
              {
                    return (string)base.GetValue(offColor);
                }
    
                set
    
                {
                 
                    base.SetValue(offColor, value);
    
                }
    
    
    
            }
    
        }
    it is not working, 
    Any ideas on how to do this?
    Wednesday, August 26, 2009 5:01 AM

Answers

  • Thnx for reply, it worked.

    Base_Control.

    BaseControl.CtrlValue.OverrideMetadata(typeof(DigitalControl), new UIPropertyMetadata("0", new PropertyChangedCallback(OnCountChanged)));

    Wednesday, August 26, 2009 8:41 AM

All replies

  • And you problem is that OnPropertyChanged is not invoked, right?

    Why not simply subscribe to the PropertyChanged event of the class (since INotifyPropertyChanged event is implemented)?
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Wednesday, August 26, 2009 5:44 AM
  • Thanx for reply
    wat i want is to pass value to ControlValue property like.............
     <TextBox Height="23" Margin="54,27,104,0" Name="textBox1" VerticalAlignment="Top" Text="8" /> 
    
    
    <D:DigitalControl ControlValue='{Binding ElementName=textBox1 ,Path=Text}'  StopStatusValue="0" StopStatusColor="Red"></D:DigitalControl>
    i can do this with BaseControl.
    but here am getting error.......

    {"Cannot create instance of 'DigitalControl' defined in assembly 'Digital_Control, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation.  Error at object 'System.Windows.Controls.Grid' in markup file 'CreateCustomeControl;component/window4.xaml' Line 10 Position 10."}
    Wednesday, August 26, 2009 5:55 AM
  • Please post the inner exception.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Wednesday, August 26, 2009 8:30 AM
  • Thnx for reply, it worked.

    Base_Control.

    BaseControl.CtrlValue.OverrideMetadata(typeof(DigitalControl), new UIPropertyMetadata("0", new PropertyChangedCallback(OnCountChanged)));

    Wednesday, August 26, 2009 8:41 AM
  • Hi Reetika,

    If you find a solution for your issue, you can mark it as answer. Doing this way, other community members can be  benificial from this thread, also it will make this thread more discoverable and searchable.

    Thanks.
    Jim Zhou -MSFT
    Tuesday, September 1, 2009 8:46 AM