locked
Change Dependecy property at design time RRS feed

  • Question

  • Hello everyone,

    I want to define theme for my usercontrol . I have created DependencyProperty in it as bellow

        public static readonly DependencyProperty DataGridThemeProperty = DependencyProperty.RegisterAttached("DataGridTheme", typeof(Theme), typeof(tecDataGrid), new PropertyMetadata(new Theme()));
            public Theme DataGridTheme
            {
                get
                {
                    return (Theme)GetValue(DataGridThemeProperty);
                }
                set
                {
                    SetValue(DataGridThemeProperty, value);
    
                    LinearGradientBrush ResBrush;
    
                    switch (value)
                    {
                        case Theme.Blue:
                            ResBrush = (LinearGradientBrush)this.Resources["grdMenu_BlueBrush"];
                            spHeader.Style = Style;
                            break;
                        case Theme.Silver:
                            ResBrush = (LinearGradientBrush)this.Resources["grdMenu_SilverBrush"];
                            spHeader.Style = Style;
                            break;
                        case Theme.Black:
                            ResBrush = (LinearGradientBrush)this.Resources["grdMenu_BlackBrush"];
                            spHeader.Background = ResBrush;
                            break;
                        default:
                            break;
                    }
    
                }
            }
    


     

    but when I add the usercontrol in my page and change it's property to any theme (DataGridTheme="Silver") ,it doesn't show me the theme in design time . I have to run the application and the theme will effect at that time . I was wondering if there is any way for changing the dependecy property even at design time.

    I appreciate any help,

    Thank you,
    Naseem

    Thursday, November 11, 2010 7:22 PM

Answers

  • Hi Naseem. I didn't check if that really would solve the issue, but I wanted to note that what you did is not the recommended pattern for dependency properties. Please:

    • Keep your CLR properties as simple as possible. E.g. remove anything from the "DataGridTheme" getter and setter except for the "GetValue" and "SetValue" calls. Nothing else.
    • Add a changed callback to the metadata you use when you register the dependency property. Put the code that you have in the setter at the moment into that callback (needs some changes due to the static nature of the callback).

    The reason for that is that the runtime usually doesn't use your CLR properties to set a dependency property. It calls the "SetValue" method directly. That means that any additional logic you have put into the setter or getter does not get executed when the runtime manipulates your dependency property.

    Friday, November 12, 2010 3:38 AM

All replies

  • Hi Naseem. I didn't check if that really would solve the issue, but I wanted to note that what you did is not the recommended pattern for dependency properties. Please:

    • Keep your CLR properties as simple as possible. E.g. remove anything from the "DataGridTheme" getter and setter except for the "GetValue" and "SetValue" calls. Nothing else.
    • Add a changed callback to the metadata you use when you register the dependency property. Put the code that you have in the setter at the moment into that callback (needs some changes due to the static nature of the callback).

    The reason for that is that the runtime usually doesn't use your CLR properties to set a dependency property. It calls the "SetValue" method directly. That means that any additional logic you have put into the setter or getter does not get executed when the runtime manipulates your dependency property.

    Friday, November 12, 2010 3:38 AM
  • Hello MisterGoodcat,

    Thank you so much for the response. I'm changing my code based on the solution you suggested me .However as call back method is static , it's giving me errors like "An object reference is required for the non-static field, method, or property ..."

     switch ((Theme)e.NewValue)
                    {
                        case Theme.Blue:
                            Style = (Style)this.Resources["grdMenu_Blue"];
                            spHeader.Style = Style;
                                                  StyleManager.SetTheme(DataGrid,new Office_BlueTheme());  
    
                            break;
                        case Theme.Silver:
                             
                            Style = (Style)this.Resources["grdMenu_Silver"];
                            spHeader.Style = Style;
    
                            StyleManager.SetTheme(DataGrid, new Office_SilverTheme()); 
                            
                            break;
    }
     
    Would you please guide me how I should change it, in order not to get errors because of Static callback method.
     
    Thank you so much,
    Naseem


     

    Sunday, November 14, 2010 5:33 PM
  • Thank you MisterGoodcat ,

    I was able to change my code as you suggested and it's working fine :)

     

     

    Sunday, November 14, 2010 6:58 PM