none
Howto know wheter Dependency property set in xaml, in code or by binding?

    Question

  • Hi All,

    Is there any way to know whether a control's Dependency Property was set in xaml?
    My goal is to differentiate whether the IsEnabled property was set in xaml
    or whether it was set from code  / a binding.

    Thanks,
    Koen

    Monday, August 30, 2010 10:36 AM

Answers

  • Hi Koen,

    I’m afraid we cannot distinguish whether the property is set in XAML or code-behind, but if it is a binding value, we can get its BindingExpression, if it is not a binding value, the BindingExpression will be null.

    We can get the BindingExpression via the FrameworkElement.GetBindingExpression method, for more information about this method, please refer the link as follows:
    http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getbindingexpression.aspx

    Hope this helps.

    Best regards,
    Kevin Pan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Koen j Sunday, September 05, 2010 8:07 AM
    Friday, September 03, 2010 7:40 AM
    Moderator

All replies

  • Hi

    Can you please elaborate your question ? As where you want to know, as you can see in code and xaml by yourself.

    Regares

    Zee

    Monday, August 30, 2010 11:14 AM
  • I want to know in framework code..
    I'm getting a list of controls from the VisualTreeHelper, and I want to know whether these controls are
    disabled. If they are disabled, I need to know whether:

    a) this was done by hard-coding IsEnabled="False" in the xaml
    b) this was done by any Binding, like IsEnabled="{Binding SomeProperty}"
        or from code SomeControl.IsEnabled=false;


    Regards,
    Koen

    Monday, August 30, 2010 11:20 AM
  • Hi Koen j

    Have you considered using DependencyPropertyDescriptor.AddValueChanged(...)

    http://msdn.microsoft.com/en-us/library/system.componentmodel.dependencypropertydescriptor_members.aspx

    I think you should be able to have an idea about source of change by looking at "sender" in the handler for this.

    Thanks,
    Muhammad
    shujaatsiddiqi.blogspot.com


    Shujaat Siddiqi
    Monday, August 30, 2010 1:15 PM
  • Thanks for your answer, but I think that's not correct Muhammad;

    When the attached ValueChanged event handler will fire, the sender will just be the control
    for which the value changed.. There will be no way to define what actually triggered changing the value..




    Regards,
    Koen

    Monday, August 30, 2010 1:19 PM
  • Hi Koen,

    I’m afraid we cannot distinguish whether the property is set in XAML or code-behind, but if it is a binding value, we can get its BindingExpression, if it is not a binding value, the BindingExpression will be null.

    We can get the BindingExpression via the FrameworkElement.GetBindingExpression method, for more information about this method, please refer the link as follows:
    http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getbindingexpression.aspx

    Hope this helps.

    Best regards,
    Kevin Pan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Koen j Sunday, September 05, 2010 8:07 AM
    Friday, September 03, 2010 7:40 AM
    Moderator